├── .gitignore ├── ChangeLog.md ├── build.gradle ├── demo ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── dhh │ │ └── demo │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── dhh │ │ │ └── demo │ │ │ ├── Api.java │ │ │ ├── MyTextView.java │ │ │ ├── MyV4Fragment.java │ │ │ ├── RxJava1Activity.java │ │ │ ├── RxJava2Activity.java │ │ │ └── RxLifecycleAPP.java │ └── res │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── activity_rx_iava2.xml │ │ ├── fragment_layout.xml │ │ └── item.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── dhh │ └── demo │ └── ExampleUnitTest.java ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── image ├── RxLifecycle.gif └── dexError.png ├── readme.md ├── rxlifecycle ├── .gitignore ├── bintray.gradle ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── dhh │ │ └── rxlifecycle │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── dhh │ │ │ └── rxlifecycle │ │ │ ├── ActivityEvent.java │ │ │ ├── ActivityRxLifecycleCallbacks.java │ │ │ ├── LifecycleFragment.java │ │ │ ├── LifecycleManager.java │ │ │ ├── LifecycleTransformer.java │ │ │ ├── LifecycleV4Fragment.java │ │ │ ├── RxLifecycle.java │ │ │ └── retrofit │ │ │ ├── HttpHelper.java │ │ │ └── RxJavaLifecycleCallAdapterFactory.java │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── dhh │ └── rxlifecycle │ └── ExampleUnitTest.java ├── rxlifecycle2 ├── .gitignore ├── bintray.gradle ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── dhh │ │ └── rxlifecycle2 │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── dhh │ │ │ └── rxlifecycle2 │ │ │ ├── ActivityEvent.java │ │ │ ├── ActivityRxLifecycleCallbacks.java │ │ │ ├── LifecycleFragment.java │ │ │ ├── LifecycleManager.java │ │ │ ├── LifecycleTransformer.java │ │ │ ├── LifecycleV4Fragment.java │ │ │ ├── RxLifecycle.java │ │ │ └── retrofit │ │ │ ├── HttpHelper.java │ │ │ └── RxJava2LifecycleCallAdapterFactory.java │ └── res │ │ └── values │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── dhh │ └── rxlifecycle2 │ └── ExampleUnitTest.java ├── rxliffecycle-retrofit.md └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | .DS_Store 5 | /build 6 | /captures 7 | .externalNativeBuild 8 | /.idea/ 9 | -------------------------------------------------------------------------------- /ChangeLog.md: -------------------------------------------------------------------------------- 1 | 2 | ### 1.6 3 | - fix:修复某种情况下导致LifecycleFragment获取失败 4 | 5 | 6 | ### 1.5 7 | - 修复生命周期绑定bug 8 | - 合并RxLifecycle-Retrofit模块到RxLifecycle模块中 9 | - 优化注入RxLifecycle逻辑 -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:2.3.3' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.5' 13 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5' 14 | } 15 | } 16 | 17 | allprojects { 18 | repositories { 19 | jcenter() 20 | maven { 21 | url 'https://maven.google.com' 22 | } 23 | } 24 | } 25 | 26 | task clean(type: Delete) { 27 | delete rootProject.buildDir 28 | } 29 | -------------------------------------------------------------------------------- /demo/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /demo/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "26.0.0" 6 | defaultConfig { 7 | applicationId "com.dhh.rxlifecycle" 8 | minSdkVersion 14 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | multiDexEnabled true 14 | } 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | } 22 | 23 | dependencies { 24 | compile fileTree(include: ['*.jar'], dir: 'libs') 25 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 26 | exclude group: 'com.android.support', module: 'support-annotations' 27 | }) 28 | testCompile 'junit:junit:4.12' 29 | compile project(path: ':rxlifecycle') 30 | //RxWebSocket 31 | compile 'com.android.support:appcompat-v7:25.3.1' 32 | compile 'com.android.support.constraint:constraint-layout:1.0.2' 33 | compile 'io.reactivex:rxandroid:1.2.1' 34 | compile 'com.android.support:recyclerview-v7:25.3.1' 35 | compile 'io.reactivex:rxjava:1.3.1' 36 | compile 'com.dhh:websocket:1.3.0' 37 | compile 'com.squareup.retrofit2:retrofit:2.3.0' 38 | compile 'com.squareup.retrofit2:converter-gson:2.3.0' 39 | compile 'com.squareup.retrofit2:adapter-rxjava:2.3.0' 40 | compile 'com.jakewharton.rxbinding:rxbinding:1.0.1' 41 | //rxjava2 42 | compile project(path: ':rxlifecycle2') 43 | compile 'io.reactivex.rxjava2:rxjava:2.1.5' 44 | compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0' 45 | compile 'io.reactivex.rxjava2:rxandroid:2.0.1' 46 | } 47 | -------------------------------------------------------------------------------- /demo/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\Develop\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /demo/src/androidTest/java/com/dhh/demo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.dhh.demo; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.dhh.rxlifecycle", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /demo/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /demo/src/main/java/com/dhh/demo/Api.java: -------------------------------------------------------------------------------- 1 | package com.dhh.demo; 2 | 3 | import okhttp3.ResponseBody; 4 | import retrofit2.http.GET; 5 | import retrofit2.http.Url; 6 | import rx.Observable; 7 | 8 | /** 9 | * Created by dhh on 2017/10/11. 10 | */ 11 | 12 | public interface Api { 13 | @GET 14 | Observable RxJava1get(@Url String url); 15 | 16 | @GET 17 | io.reactivex.Observable RxJava2get(@Url String url); 18 | } 19 | -------------------------------------------------------------------------------- /demo/src/main/java/com/dhh/demo/MyTextView.java: -------------------------------------------------------------------------------- 1 | package com.dhh.demo; 2 | 3 | import android.content.Context; 4 | import android.support.annotation.Nullable; 5 | import android.util.AttributeSet; 6 | import android.widget.TextView; 7 | 8 | import com.dhh.rxlifecycle.RxLifecycle; 9 | 10 | import java.util.concurrent.TimeUnit; 11 | 12 | import rx.Observable; 13 | import rx.android.schedulers.AndroidSchedulers; 14 | import rx.functions.Action1; 15 | 16 | /** 17 | * Created by dhh on 2017/9/27. 18 | */ 19 | 20 | public class MyTextView extends TextView { 21 | public MyTextView(Context context) { 22 | super(context); 23 | } 24 | 25 | public MyTextView(Context context, @Nullable AttributeSet attrs) { 26 | super(context, attrs); 27 | } 28 | 29 | public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 30 | super(context, attrs, defStyleAttr); 31 | } 32 | 33 | public void RxLifeCycleSetText(final String text) { 34 | Observable.timer(5, TimeUnit.SECONDS) 35 | .compose(RxLifecycle.with(this).bindToLifecycle()) 36 | .observeOn(AndroidSchedulers.mainThread()) 37 | .subscribe(new Action1() { 38 | @Override 39 | public void call(Long aLong) { 40 | setText(text); 41 | } 42 | }); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /demo/src/main/java/com/dhh/demo/MyV4Fragment.java: -------------------------------------------------------------------------------- 1 | package com.dhh.demo; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.Nullable; 5 | import android.support.v4.app.Fragment; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | 10 | import com.dhh.rxlifecycle.RxLifecycle; 11 | 12 | import java.util.concurrent.TimeUnit; 13 | 14 | import rx.Observable; 15 | 16 | /** 17 | * Created by dhh on 2017/9/27. 18 | */ 19 | 20 | public class MyV4Fragment extends Fragment { 21 | private MyTextView myTextView; 22 | 23 | @Override 24 | public void onCreate(@Nullable Bundle savedInstanceState) { 25 | super.onCreate(savedInstanceState); 26 | } 27 | 28 | @Nullable 29 | @Override 30 | public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 31 | return inflater.inflate(R.layout.fragment_layout, null); 32 | } 33 | 34 | @Override 35 | public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 36 | myTextView = (MyTextView) view.findViewById(R.id.myTextView); 37 | Observable.timer(10, TimeUnit.SECONDS) 38 | .compose(RxLifecycle.with(this).bindToLifecycle()) 39 | .subscribe(); 40 | 41 | } 42 | 43 | @Override 44 | public void onStart() { 45 | super.onStart(); 46 | myTextView.RxLifeCycleSetText("dhhAndroid"); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /demo/src/main/java/com/dhh/demo/RxJava1Activity.java: -------------------------------------------------------------------------------- 1 | package com.dhh.demo; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.util.Log; 6 | import android.widget.Button; 7 | import android.widget.Toast; 8 | 9 | import com.dhh.rxlifecycle.ActivityEvent; 10 | import com.dhh.rxlifecycle.LifecycleManager; 11 | import com.dhh.rxlifecycle.LifecycleTransformer; 12 | import com.dhh.rxlifecycle.RxLifecycle; 13 | import com.dhh.rxlifecycle.retrofit.HttpHelper; 14 | import com.dhh.websocket.RxWebSocketUtil; 15 | import com.jakewharton.rxbinding.view.RxView; 16 | 17 | import java.io.IOException; 18 | import java.util.concurrent.TimeUnit; 19 | 20 | import okhttp3.OkHttpClient; 21 | import okhttp3.ResponseBody; 22 | import retrofit2.converter.gson.GsonConverterFactory; 23 | import rx.Observable; 24 | import rx.Subscriber; 25 | import rx.Subscription; 26 | import rx.android.schedulers.AndroidSchedulers; 27 | import rx.functions.Action0; 28 | import rx.functions.Action1; 29 | import rx.functions.Func1; 30 | 31 | public class RxJava1Activity extends AppCompatActivity { 32 | 33 | private LifecycleManager mLifecycleManager; 34 | private MyTextView myTextView; 35 | private Subscription mSubscription; 36 | 37 | @Override 38 | protected void onCreate(Bundle savedInstanceState) { 39 | super.onCreate(savedInstanceState); 40 | setContentView(R.layout.activity_main); 41 | // startActivity(new Intent(this,RxJava2Activity.class)); 42 | // finish(); 43 | RxLifecycle.injectRxLifecycle(this); 44 | initView(); 45 | mLifecycleManager = RxLifecycle.with(this); 46 | Observable.just(1) 47 | .compose(bindToLifecycle()) 48 | .subscribe(); 49 | Observable.just("34") 50 | .compose(this.bindToLifecycle()) 51 | .subscribe(); 52 | 53 | mSubscription = Observable.just(1).subscribe(); 54 | 55 | 56 | //RxLifecycle-Retrofit 模块代码demo 57 | 58 | //初始化HttpHelper 59 | HttpHelper.getInstance().setBaseUrl("https://github.com/dhhAndroid/"); 60 | HttpHelper.getInstance().setClient(new OkHttpClient()); 61 | HttpHelper.getInstance().setConverterFactory(GsonConverterFactory.create()); 62 | final Api api = HttpHelper.getInstance().createWithLifecycleManager(Api.class, RxLifecycle.with(this)); 63 | final Button button = (Button) findViewById(R.id.button); 64 | RxView.clicks(button) 65 | .flatMap(new Func1>() { 66 | @Override 67 | public Observable call(Void aVoid) { 68 | return Observable.interval(0, 1, TimeUnit.SECONDS, AndroidSchedulers.mainThread()); 69 | } 70 | }) 71 | .take(6) 72 | .map(new Func1() { 73 | @Override 74 | public Long call(Long aLong) { 75 | return 5 - aLong; 76 | } 77 | }) 78 | .takeFirst(new Func1() { 79 | @Override 80 | public Boolean call(Long aLong) { 81 | button.setText(aLong + "秒后开始网络请求"); 82 | return aLong == 0; 83 | } 84 | }) 85 | .flatMap(new Func1>() { 86 | @Override 87 | public Observable call(Long aLong) { 88 | return api.RxJava1get("https://github.com/dhhAndroid/RxLifecycle"); 89 | } 90 | }) 91 | .map(new Func1() { 92 | @Override 93 | public String call(ResponseBody body) { 94 | try { 95 | return body.string(); 96 | } catch (IOException e) { 97 | return "解析错误 !"; 98 | } 99 | } 100 | }) 101 | .doOnUnsubscribe(new Action0() { 102 | @Override 103 | public void call() { 104 | Toast.makeText(RxJava1Activity.this, "网络请求取消/完成了 !", Toast.LENGTH_SHORT).show(); 105 | } 106 | }) 107 | .observeOn(AndroidSchedulers.mainThread()) 108 | .subscribe(new Action1() { 109 | @Override 110 | public void call(String s) { 111 | Log.d("RxJava1Activity", s); 112 | button.setText("网络请求完成!"); 113 | Toast.makeText(RxJava1Activity.this, s, Toast.LENGTH_SHORT).show(); 114 | } 115 | }); 116 | api.RxJava1get("https://github.com/dhhAndroid/RxLifecycle") 117 | .compose(RxLifecycle.with(this).bindOnDestroy()) 118 | .observeOn(AndroidSchedulers.mainThread()) 119 | .subscribe(new Action1() { 120 | @Override 121 | public void call(ResponseBody body) { 122 | 123 | } 124 | }); 125 | } 126 | 127 | private void unSubscribeTest() { 128 | Observable.just(1, 23, 434, 5454, 343, 346, 56, 67, 4, -1) 129 | //取前五个就注销 130 | .take(5) 131 | //直到条件满足,注销 132 | .takeUntil(new Func1() { 133 | @Override 134 | public Boolean call(Integer integer) { 135 | return integer > 66666; 136 | } 137 | }) 138 | //直到另外一个Observable发送数据就注销,本库主要用的这个操作符 139 | .takeUntil(Observable.just(1)) 140 | .first(new Func1() { 141 | @Override 142 | public Boolean call(Integer integer) { 143 | return integer == 111; 144 | } 145 | }) 146 | .map(new Func1() { 147 | @Override 148 | public Integer call(Integer integer) { 149 | if (integer < 0) { 150 | //抛异常注销,这种用法在我另外一个库RxProgressManager使用到 151 | throw new RuntimeException("数据不能小于0"); 152 | } 153 | return integer; 154 | } 155 | }) 156 | .subscribe(new Subscriber() { 157 | @Override 158 | public void onCompleted() { 159 | 160 | } 161 | 162 | @Override 163 | public void onError(Throwable e) { 164 | 165 | } 166 | 167 | @Override 168 | public void onNext(Integer integer) { 169 | if (integer == 666) { 170 | //当满足条件注销 171 | unsubscribe(); 172 | } 173 | } 174 | }); 175 | } 176 | 177 | private LifecycleTransformer bindToLifecycle() { 178 | return RxLifecycle.with(this).bindToLifecycle(); 179 | } 180 | 181 | private LifecycleTransformer bindOnDestroy() { 182 | return RxLifecycle.with(this).bindOnDestroy(); 183 | } 184 | 185 | private LifecycleTransformer bindUntilEvent(ActivityEvent event) { 186 | return RxLifecycle.with(this).bindUntilEvent(event); 187 | } 188 | 189 | @Override 190 | protected void onStart() { 191 | super.onStart(); 192 | Observable.just(1) 193 | .compose(bindToLifecycle()) 194 | .subscribe(); 195 | myTextView.RxLifeCycleSetText("dhhAndroid"); 196 | 197 | } 198 | 199 | @Override 200 | protected void onResume() { 201 | super.onResume(); 202 | Observable.just(1) 203 | .compose(RxLifecycle.with(this).bindToLifecycle()) 204 | .subscribe(); 205 | RxWebSocketUtil.getInstance().setShowLog(BuildConfig.DEBUG); 206 | RxWebSocketUtil.getInstance().getWebSocketString("ws://127.0.0.1:8089") 207 | .compose(RxLifecycle.with(this).bindToLifecycle()) 208 | .subscribe(new Action1() { 209 | @Override 210 | public void call(String s) { 211 | 212 | } 213 | }); 214 | } 215 | 216 | @Override 217 | protected void onPause() { 218 | super.onPause(); 219 | Observable.just("dhhAndroid") 220 | .compose(RxLifecycle.with(this).bindOnDestroy()) 221 | .subscribe(); 222 | } 223 | 224 | @Override 225 | protected void onStop() { 226 | super.onStop(); 227 | Observable.just("dhhAndroid") 228 | .compose(RxLifecycle.with(this).bindOnDestroy()) 229 | .subscribe(); 230 | Observable.timer(10, TimeUnit.SECONDS) 231 | .compose(mLifecycleManager.bindToLifecycle()) 232 | .subscribe(); 233 | test(); 234 | 235 | } 236 | 237 | @Override 238 | protected void onDestroy() { 239 | super.onDestroy(); 240 | if (mSubscription != null && !mSubscription.isUnsubscribed()) { 241 | mSubscription.unsubscribe(); 242 | } 243 | } 244 | 245 | private void test() { 246 | Observable.timer(10, TimeUnit.SECONDS) 247 | .doOnSubscribe(new Action0() { 248 | @Override 249 | public void call() { 250 | Log.d("RxJava1Activity", "注册"); 251 | } 252 | }) 253 | .doOnUnsubscribe(new Action0() { 254 | @Override 255 | public void call() { 256 | Log.d("RxJava1Activity", "注销"); 257 | } 258 | }) 259 | .compose(RxLifecycle.with(this).bindOnDestroy()) 260 | .subscribe(); 261 | Observable.timer(10, TimeUnit.SECONDS) 262 | .compose(RxLifecycle.with(this).bindToLifecycle()) 263 | .subscribe(); 264 | Observable.timer(10, TimeUnit.SECONDS) 265 | //当activity onstop 注销 266 | .compose(RxLifecycle.with(this).bindUntilEvent(ActivityEvent.onStop)) 267 | .subscribe(); 268 | } 269 | 270 | private void initView() { 271 | myTextView = (MyTextView) findViewById(R.id.myTextView); 272 | } 273 | } 274 | -------------------------------------------------------------------------------- /demo/src/main/java/com/dhh/demo/RxJava2Activity.java: -------------------------------------------------------------------------------- 1 | package com.dhh.demo; 2 | 3 | import android.os.Bundle; 4 | import android.os.Looper; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.util.Log; 7 | import android.widget.Toast; 8 | 9 | import com.dhh.rxlifecycle2.LifecycleManager; 10 | import com.dhh.rxlifecycle2.RxLifecycle; 11 | import com.dhh.rxlifecycle2.retrofit.HttpHelper; 12 | 13 | import java.util.concurrent.TimeUnit; 14 | 15 | import io.reactivex.Observable; 16 | import io.reactivex.ObservableSource; 17 | import io.reactivex.android.schedulers.AndroidSchedulers; 18 | import io.reactivex.annotations.NonNull; 19 | import io.reactivex.functions.Action; 20 | import io.reactivex.functions.Consumer; 21 | import io.reactivex.functions.Function; 22 | import okhttp3.OkHttpClient; 23 | import okhttp3.ResponseBody; 24 | import retrofit2.converter.gson.GsonConverterFactory; 25 | 26 | public class RxJava2Activity extends AppCompatActivity { 27 | 28 | @Override 29 | protected void onCreate(Bundle savedInstanceState) { 30 | super.onCreate(savedInstanceState); 31 | setContentView(R.layout.activity_rx_iava2); 32 | LifecycleManager lifecycleManager = RxLifecycle.with(this); 33 | Observable.just(2) 34 | .compose(lifecycleManager.bindOnDestroy()) 35 | .subscribe(); 36 | //初始化HttpHelper 37 | HttpHelper.getInstance().setBaseUrl("https://github.com/dhhAndroid/"); 38 | HttpHelper.getInstance().setClient(new OkHttpClient()); 39 | HttpHelper.getInstance().setConverterFactory(GsonConverterFactory.create()); 40 | final Api api = HttpHelper.getInstance().createWithLifecycleManager(Api.class, lifecycleManager); 41 | Observable.timer(3, TimeUnit.SECONDS) 42 | .flatMap(new Function>() { 43 | @Override 44 | public ObservableSource apply(@NonNull Long aLong) throws Exception { 45 | return api.RxJava2get("https://github.com/dhhAndroid/RxLifecycle/blob/master/readme.md") 46 | .map(new Function() { 47 | @Override 48 | public String apply(@NonNull ResponseBody body) throws Exception { 49 | return body.string(); 50 | } 51 | }); 52 | } 53 | }) 54 | .doOnDispose(new Action() { 55 | @Override 56 | public void run() throws Exception { 57 | Looper.prepare(); 58 | Toast.makeText(RxJava2Activity.this, "网络请求取消/完成了 !", Toast.LENGTH_SHORT).show(); 59 | Looper.loop(); 60 | } 61 | }) 62 | .observeOn(AndroidSchedulers.mainThread()) 63 | .subscribe(new Consumer() { 64 | @Override 65 | public void accept(String s) throws Exception { 66 | Log.d("RxJava2Activity", s); 67 | Toast.makeText(RxJava2Activity.this, s, Toast.LENGTH_SHORT).show(); 68 | } 69 | }); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /demo/src/main/java/com/dhh/demo/RxLifecycleAPP.java: -------------------------------------------------------------------------------- 1 | package com.dhh.demo; 2 | 3 | import android.app.Application; 4 | 5 | import com.dhh.rxlifecycle.RxLifecycle; 6 | import com.dhh.rxlifecycle.retrofit.RxJavaLifecycleCallAdapterFactory; 7 | import com.dhh.rxlifecycle2.retrofit.RxJava2LifecycleCallAdapterFactory; 8 | 9 | import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory; 10 | import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; 11 | import rx.schedulers.Schedulers; 12 | 13 | /** 14 | * Created by dhh on 2017/9/27. 15 | */ 16 | 17 | public class RxLifecycleAPP extends Application { 18 | @Override 19 | public void onCreate() { 20 | super.onCreate(); 21 | RxLifecycle.injectRxLifecycle(this); 22 | com.dhh.rxlifecycle2.RxLifecycle.injectRxLifecycle(this); 23 | 24 | //如果你有一个自定义的XXXRxJavaCallAdapterFactory,这样注入 25 | // 假如这是你自己定义的XXXRxJavaCallAdapterFactory 26 | RxJavaCallAdapterFactory yourFactory = RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io()); 27 | //RxJava1 CallAdpater 28 | RxJavaLifecycleCallAdapterFactory.injectCallAdapterFactory(yourFactory); 29 | //RxJava2 CallAdpater 30 | RxJava2LifecycleCallAdapterFactory.injectCallAdapterFactory(RxJava2CallAdapterFactory.create()); 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /demo/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 19 | 20 | 33 | 34 | 35 | 36 | 46 | 47 |