轻量级无侵入性的RxJava自动注销库
//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);
}
}
@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();
}
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();
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();
....
}
}
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.