Jetpack是Google为Android开发提供的组件库.旨在帮助开发者更快更好的开发App程序. 它提供了一系列工具,库和架构组件涵盖了UI展示数据管理,和后台服务到设备兼容的各个方面.
LifeCycle
用于管理组件的生命周期,使开发者能够更方便地编写与组件生命周期相关的代码,实现代码解耦和资源的合理管理。
上篇文章从源码分析了Lifecycle LifecycleRegistry 和LifecycleOwner 的代码实现,这篇文章我们梳理一下 他们的关系以及调用
1:具体实现功能
Lifecycle
是一个抽象类 提供了抽象方法和属性
- 声明了添加和移除观察的方法
- 声明了事件以及事件状态
- 提供了一个生命周期的协程作用域
- 声明了一个事件流
- 声明了一个状态流
LifecycleRegistry
继承了Lifecycle 实现了具体的功能
LifecycleOwner
是一个接口,维护了一个Lifecycle的对象 Activity/Fragment 等控件通过实现LifecycleOwner 持有了Lifecycle 并创建了一个LifecycleRegistry 对象来分发其生命周期的状态
2:三者关系
Lifecycle是一个抽象类
LifecycleOwner是一个接口维护了一个Lifecycle变量
LifecycleRegistry继承Lifecycle且通过构造维护了一个 LifecycleOwner对象
3:Activity生命周期观察的具体实现
3.1 Activity 创建LifecycleRegistry并且 将LifecycleRegistry对象赋值给LifecycleOwner的lifecycle
3.2 创建LifecycleRegistry对象 弱引用维护LifecycleOwner
的addObserver()
方法将
3.3 创建Lifecycle 创建协程对象并且调用LifecycleRegistry对象的观察者对象添加到LifecycleRegistr中mapozhong
注意 LifecycleCoroutineScopeImpl
即实现了LifecycleCoroutineScope
又实现了LifecycleEventObserver
所以它即是一个协程作用域 又是一个状态变化的观察者
所以 lifecycle.addObserver(this@LifecycleCoroutineScopeImpl)
是讲一个观察者传递了出去
kotlin
//即实现了LifecycleCoroutineScope又实现了LifecycleEventObserver
internal class LifecycleCoroutineScopeImpl(
override val lifecycle: Lifecycle,
override val coroutineContext: CoroutineContext
) : LifecycleCoroutineScope(), LifecycleEventObserver {
init {
//销毁状态取消协程
if (lifecycle.currentState == Lifecycle.State.DESTROYED) {
coroutineContext.cancel()
}
}
fun register() {
launch(Dispatchers.Main.immediate) {
if (lifecycle.currentState >= Lifecycle.State.INITIALIZED) {
lifecycle.addObserver(this@LifecycleCoroutineScopeImpl)
} else {
coroutineContext.cancel()
}
}
}
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
//销毁状态的时候取消协程
if (lifecycle.currentState <= Lifecycle.State.DESTROYED) {
lifecycle.removeObserver(this)
coroutineContext.cancel()
}
}
}
3.4 Lifecycle
调用LifecycleRegistry
的addObserver()
方法将观察者添加到LifecycleRegistry
维护的observerMap
中
4: FragmentActivity 生命周期变化的通知流程图
java
package androidx.fragment.app;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import androidx.activity.ComponentActivity;
import androidx.annotation.CallSuper;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleRegistry;
import androidx.savedstate.SavedStateRegistry;
public class FragmentActivity extends ComponentActivity {
static final String FRAGMENTS_TAG = "android:support:fragments";
private final FragmentController mFragments = FragmentController.createController(new HostCallbacks());
private final LifecycleRegistry mFragmentLifecycleRegistry = new LifecycleRegistry(this);
public FragmentActivity() {
super();
init();
}
public FragmentActivity(int contentLayoutId) {
super(contentLayoutId);
init();
}
private void init() {
getSavedStateRegistry().registerSavedStateProvider(FRAGMENTS_TAG,
new SavedStateRegistry.SavedStateProvider() {
@Override
public Bundle saveState() {
Bundle outState = new Bundle();
markFragmentsCreated();
// 通知 mFragmentLifecycleRegistry,Fragment 的生命周期进入 ON_STOP 状态
mFragmentLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
android.os.Parcelable p = mFragments.saveAllState();
if (p != null) {
outState.putParcelable(FRAGMENTS_TAG, p);
}
return outState;
}
});
addOnContextAvailableListener(context -> {
mFragments.attachHost(null);
Bundle savedInstanceState = getSavedStateRegistry()
.consumeRestoredStateForKey(FRAGMENTS_TAG);
if (savedInstanceState != null) {
android.os.Parcelable p = savedInstanceState.getParcelable(FRAGMENTS_TAG);
mFragments.restoreSaveState(p);
}
});
}
@Override
@CallSuper
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 通知 mFragmentLifecycleRegistry,Fragment 的生命周期进入 ON_CREATE 状态
mFragmentLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
mFragments.dispatchCreate();
}
@Override
@CallSuper
protected void onStart() {
mFragments.noteStateNotSaved();
super.onStart();
markFragmentsCreated();
// 通知 mFragmentLifecycleRegistry,Fragment 的生命周期进入 ON_START 状态
mFragmentLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
mFragments.dispatchStart();
}
@Override
@CallSuper
protected void onResume() {
mFragments.noteStateNotSaved();
super.onResume();
}
@Override
@CallSuper
protected void onPostResume() {
super.onPostResume();
onResumeFragments();
}
protected void onResumeFragments() {
// 通知 mFragmentLifecycleRegistry,Fragment 的生命周期进入 ON_RESUME 状态
mFragmentLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME);
mFragments.dispatchResume();
}
@Override
@CallSuper
protected void onPause() {
super.onPause();
mFragments.dispatchPause();
// 通知 mFragmentLifecycleRegistry,Fragment 的生命周期进入 ON_PAUSE 状态
mFragmentLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE);
}
@Override
@CallSuper
protected void onStop() {
super.onStop();
markFragmentsCreated();
mFragments.dispatchStop();
// 通知 mFragmentLifecycleRegistry,Fragment 的生命周期进入 ON_STOP 状态
mFragmentLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
}
@Override
@CallSuper
protected void onDestroy() {
super.onDestroy();
mFragments.dispatchDestroy();
// 通知 mFragmentLifecycleRegistry,Fragment 的生命周期进入 ON_DESTROY 状态
mFragmentLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
}
private void markFragmentsCreated() {
boolean reiterate;
do {
reiterate = markState(mFragments.getSupportFragmentManager(), Lifecycle.State.CREATED);
} while (reiterate);
}
private static boolean markState(FragmentManager manager, Lifecycle.State state
总结
Android Jetpack学习(二)---LifeCycle,LifecycleRegistry和LifecycleOwner 源码梳理