【停止维护,请查看新项目RxLife】
原理解析:戳我试试
- 轻量:整个库只有24k.
- 入侵性低:不改变原来项目架构,继承体系,仅需OBservable事件流上加入本库即可.
- 适用于基于RxJava开发的任何三方库,如RxBus,RxBinding,以及本人的另外两个库,RxWebSocket(WebSocket自动重连库),RxProgressManager(网络层基于okhttp的上传下载进度监听库) 等等.
- 对于RxJava+Retrofit请求框架,有RxLifecycle-Retrofit拓展模块,从retrofit层自动注销网络情况(统一绑定到Activity销毁时取消所有正在进行的网络请求).
- 支持RxJava1.x和RxJava2.x.
//RxJava1版本
compile 'com.dhh:rxlifecycle:1.6'
//RxJava2版本
compile 'com.dhh:rxlifecycle2:1.6'
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RxLifecycle.injectRxLifecycle(this);
}
private <T> LifecycleTransformer<T> bindToLifecycle() {
return RxLifecycle.with(this).bindToLifecycle();
}
private <T> LifecycleTransformer<T> bindOnDestroy() {
return RxLifecycle.with(this).bindOnDestroy();
}
private <T> LifecycleTransformer<T> bindUntilEvent(ActivityEvent event) {
return RxLifecycle.with(this).bindUntilEvent(event);
}
//use
@Override
protected void onStart() {
super.onStart();
Observable.just(1)
//use
.compose(bindToLifecycle())
.subscribe();
}
public class RxLifecycleAPP extends Application {
@Override
public void onCreate() {
super.onCreate();
RxLifecycle.injectRxLifecycle(this);
}
}
2. 如果你不在Activity的"onPause"生命周期及其以后的生命周期里订阅一个Observable,注入RxLifecycle的步骤可以省略不做.如果在Activity的"onPause"生命周期及其以后的生命周期里订阅一个Observable,并且使用RxLifecycle.with(this).bindToLifecycle(),必须进行RxLifecycle注入步奏.代码说明:
@Override
protected void onPause() {
super.onPause();
Observable.just("dhhAndroid")
//其他方式绑定不用预先注入
.compose(RxLifecycle.with(this).<String>bindOnDestroy())
.subscribe();
Observable.just(1)
//在onPause及其以后生命周期,使用bindToLifecycle()必须先注入RxLifecycle
.compose(RxLifecycle.with(this).<Integer>bindToLifecycle())
.subscribe();
}
如果你使用的是RxJava+Retrofit网络框架,有更好的选择方式,项目里提供了Retrofit模块,从Retrofit层自动注销RxJava:RxLifecycle-Retrofit模块
Observable.timer(10, TimeUnit.SECONDS)
//自动判断生命周期
.compose(RxLifecycle.with(this).<Long>bindToLifecycle())
.subscribe();
Observable.timer(10, TimeUnit.SECONDS)
//当activity onStop 注销
.compose(RxLifecycle.with(this).<Long>bindUntilEvent(ActivityEvent.onStop))
.subscribe();
Observable.just("dhhAndroid")
//当activity onDestroy 注销
.compose(RxLifecycle.with(this).<String>bindOnDestroy())
.subscribe();
如果你在自定义view的时候里面使用的RxJava,以及View内部有retrofit+RxJava的网络访问,以及RxJava操作的耗时数据转换,同样支持一行代码管理RxJava自动注销,用法和在activity和fragment里一样:
public class MyView extends View {
//other...
public void doSomething(){
Observable.timer(10, TimeUnit.SECONDS)
//自动判断生命周期
.compose(RxLifecycle.with(this).<Long>bindToLifecycle())
.subscribe();
Observable.timer(10, TimeUnit.SECONDS)
//当activity onStop 注销
.compose(RxLifecycle.with(this).<Long>bindUntilEvent(ActivityEvent.onStop))
.subscribe();
Observable.just("dhhAndroid")
//当activity onDestroy 注销
.compose(RxLifecycle.with(this).<String>bindOnDestroy())
.subscribe();
....
}
}
在MVP架构或者其他地方使用RxLifecycle时,仅需确保所使用的地方能获取到一个能转化成Activity的Context即可. 项目里我写了一个with重载方法可传入任意对象,只要能转化成Context,或者通过反射能获取到所传对象的成员变量有能转化成Context(Activity),也可实现RxJava生命周期自动绑定,但考虑到性能问题暂未开放(private方法).代码如下:
private static LifecycleManager with(Object object) {
if (object instanceof Context) {
return with((Context) object);
}
for (Field field : object.getClass().getDeclaredFields()) {
try {
field.setAccessible(true);
Object value = field.get(object);
if (value instanceof Context) {
return with((Context) value);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
throw new ClassCastException(object.getClass().getSimpleName() + " can\'t convert Context !");
}
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.