使用步骤;
第一步; 添加依赖
-
api 'io.reactivex.rxjava2:rxandroid:2.1.0'
-
api 'io.reactivex.rxjava2:rxjava:2.1.16'
-
api 'com.uber.autodispose:autodispose:0.7.0'
-
api 'com.uber.autodispose:autodispose-android-archcomponents:0.7.0'
第二步:
添加类 RxLifecycleUtils.java 代码如下:
public class RxLifecycleUtils {
private RxLifecycleUtils() {
throw new IllegalStateException("Can't instance the RxLifecycleUtils");
}
public static <T> AutoDisposeConverter<T> bindLifecycle(LifecycleOwner lifecycleOwner) {
return AutoDispose.autoDisposable(
AndroidLifecycleScopeProvider.from(lifecycleOwner.getLifecycle(), Lifecycle.Event.ON_DESTROY)
);
}
3.在BaseActivity 和BaseFragment中添加
public LifecycleOwner getLifecycleOwner(){
return this;
}
3.1 在BasePresenter中添加
protected <T> AutoDisposeConverter<T> bindLifecycle() {
if (null == getLifecycleOwner())
throw new NullPointerException("lifecycleOwner == null");
return RxLifecycleUtils.bindLifecycle(getLifecycleOwner());
}
public LifecycleOwner getLifecycleOwner() {
return getV().getLifecycleOwner();
}
4. 使用案例;
public class Tes