添加依赖
implementation 'com.trello.rxlifecycle2:rxlifecycle:2.2.1'
implementation 'com.trello.rxlifecycle2:rxlifecycle-android-lifecycle:2.2.1'
在rxlifecycle依赖包下游如下几个关键类
- RxLifecycle
- LifecycleProvider
- LifecycleTransformaer
在rxlifecycle-android-lifecycle依赖包下有如下几个关键类
- AndroidLifecycle
- RxLifecycleAndroidLifecycle
不直接使用RxLifecycle
,而是使用AndroidLifecycle
.
如何创建AndroidLifecycle
.
//AndroidLifecycle.java
LifecycleProvider<Lifecycle.Event> provider = AndroidLifecycle.createLifecycleProvider(this);
AndroidLifecycle
实现了android.arch.lifecycle.LifecycleObserver
.可以通过注解@OnLifecycleEvent
来监听对应的Lifecycle.Event
.
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
void onEvent(LifecycleOwner owner, Lifecycle.Event event) {
//向下游传递
lifecycleSubject.onNext(event);
if (event == Lifecycle.Event.ON_DESTROY) {
//解除观察者
owner.getLifecycle().removeObserver(this);
}
}
这里的lifecycleSubject
是一个BehaviorSubject
对象.既可以作为观察者,也可以作为被观察对象使用.当监听到对应的Lifecycle.Event
时,就会通过lifecycleSubject.onNext(event);
向下游传递.
在AndroidLifecycle
中定义了两个绑定相关的方法.通过这两个绑定方法,将lifecycleSubject
与下游连接起来,才能确保lifecycleSubject
携带的信息能传递给下游.
/**
* 绑定某个具体的生命周期环节
* event具体为
* ON_CREATE
* ON_START
* ON_RESUME
* ON_PAUSE
* ON_STOP
* ON_DESTROY
* ON_ANY
*/
public <T> LifecycleTransformer<T> bindUntilEvent(@NonNull Lifecycle.Event event) {
return RxLifecycle.bindUntilEvent(lifecycleSubject, event);
}
/**
* 绑定到生命周期
*/
public <T> LifecycleTransformer<T> bindToLifecycle() {
return RxLifecycleAndroidLifecycle.bindLifecycle(lifecycleSubject);
}
第一个方法使用到了RxLifecycle.bindUntilEvent
方法.
public static <T, R> LifecycleTransformer<T> bindUntilEvent(@Nonnull final Observable<R> lifecycle,
@Nonnull final R event) {
checkNotNull(lifecycle, "lifecycle == null");
checkNotNull(event, "event == null");
return bind(takeUntilEvent(lifecycle, event));
}
这里的takeUntilEvent
方法是判断lifecycle
所携带的event
是否与参数event
一致.bind
方法源码如下:
public static <T, R> LifecycleTransformer<T> bind(@Nonnull final Observable<R> lifecycle) {
return new LifecycleTransformer<>(lifecycle);
}
创建了一个LifecycleTransformer
对象.
public final class LifecycleTransformer<T> implements ObservableTransformer<T, T>,
FlowableTransformer<T, T>,
SingleTransformer<T, T>,
MaybeTransformer<T, T>,
CompletableTransformer{
final Observable<?> observable;
LifecycleTransformer(Observable<?> observable) {
checkNotNull(observable, "observable == null");
this.observable = observable;
}
@Override
public ObservableSource<T> apply(Observable<T> upstream) {
return upstream.takeUntil(observable);
}
@Override
public Publisher<T> apply(Flowable<T> upstream) {
return upstream.takeUntil(observable.toFlowable(BackpressureStrategy.LATEST));
}
@Override
public SingleSource<T> apply(Single<T> upstream) {
return upstream.takeUntil(observable.firstOrError());
}
@Override
public MaybeSource<T> apply(Maybe<T> upstream) {
return upstream.takeUntil(observable.firstElement());
}
@Override
public CompletableSource apply(Completable upstream) {
return Completable.ambArray(upstream, observable.flatMapCompletable(Functions.CANCEL_COMPLETABLE));
}
}
先说一下takeUntil
操作符的作用.
ObservableA.takeUntil(ObservableB);
当ObservableB
开始发射数据,ObservableA
停止发射数据.
那么对于LifecycleTransformer
,当observable
开始发射数据,upstream
就会停止发射数据.这里的observable
就是AndroidLifecycle
中的BehaviorSubject
.而upstream
就是我们自己的数据源.
LifecycleTransformer
通过与RxJava2
的操作符compose
结合使用.
val lifecycleProvider = AndroidLifecycle.createLifecycleProvider(this)
observable.compose(lifecycleProvider.bindUntilEvent(Lifecycle.Event.ON_DESTROY))
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。