在FragmentActivity类中初始化了LifecycleRegistry,因此观察者LifecycleCoroutineScopeImpl和被观察者LifecycleOwner建立了关联,如下:
private final LifecycleRegistry mFragmentLifecycleRegistry = new LifecycleRegistry(this);
所以,只要生命周期有变化,就会通知到观察者,如下所示:
看下这段代码其实就是把生命周期事件分发通知外部的观察者,如下:
public void handleLifecycleEvent(@NonNull Lifecycle.Event event) {
enforceMainThreadIfNeeded("handleLifecycleEvent");
moveToState(event.getTargetState());
}
1.lifecycle中是如何管理协程的生命周期?
看一个下面一个例子在AppCompatActivity中,用户点击按钮获取用户信息,但是网络请求还没回来,此时用户快速关闭页面,这个时候下面那段代码会有内存泄露风险吗?答案是不会,那它是怎么做到的呢?后面一一剖析。
lifecycleScope是ktx中的一个拓展属性,如下:
同时,coroutineScope又是Lifecycle中的一个拓展属性,返回一个带有生命周期的协程作用域LifecycleCoroutineScope,如下:
这是使用了CAS机制保证LifecycleCoroutineScopeImpl类创建的线程安全,LifecycleCoroutineScopeImpl的类层次结构如下: LifecycleCoroutineScopeImpl -> LifecycleEventObserver -> LifecycleObserver,所以LifecycleCoroutineScopeImpl其实是一个观察者对象,注入的过程在newScope.register()方法,如下:
Lifecycle是一个抽象类,表示具有生命周期类的抽象,它的实现类是LifecycleRegistry,在内部实现了addObserver和removeObserver,观察者的存储是一个FastSafeIterableMap,如下:
被观察者其实是一个LifecycleOwner接口,其实现类包括Fragment和ComponentActivity,以Activity为例,类的继承关系如下: MainActivity2(业务UI)-> AppCompatActivity -> FragmentActivity -> ComponentActivity -> LifecycleOwner
在FragmentActivity类中初始化了LifecycleRegistry,因此观察者LifecycleCoroutineScopeImpl和被观察者LifecycleOwner建立了关联,如下:
private final LifecycleRegistry mFragmentLifecycleRegistry = new LifecycleRegistry(this);
所以,只要生命周期有变化,就会通知到观察者,如下所示:
看下这段代码其实就是把生命周期事件分发通知外部的观察者,如下:
moveToState方法最终会调到sync()方法,如下:
sync方法的调用如下:
在backwardPass或者forwardPass方法中都是做遍历操作,挨个通知观察者,如下所示:
通过observer.dispatchEvent(lifecycleOwner, event); 最终调用到LifecycleCoroutineScopeImpl类中的onStateChanged方法,如下代码所示:
接着往下走,看看在LifecycleCoroutineScopeImpl中onStateChanged方法中做了什么,如下:
在这里收到销毁生命周期回调时,移除了协程观察者,保证了不会内存泄露。
2.lifecycle中是如何管理livedata的生命周期?
livedata注册观察者的过程,其实最终也是调用LifecycleRegistry来实现的原理和协程生命周期管理一个思路,如下:
接下来看一下LifecycleBoundObserver的继承关系,如下: LifecycleBoundObserver -> ObserverWrapper -> LifecycleEventObserver -> LifecycleObserver 其实监听生命周期的回调是在LifecycleBoundObserver类中的onStateChanged方法,具体逻辑如下: