请点赞,你的点赞对我意义重大,满足下我的虚荣心。
🔥 Hi,我是小彭。本文已收录到 GitHub · Android-NoteBook 中。这里有 Android 进阶成长知识体系,有志同道合的朋友,关注公众号 [彭旭锐] 跟我一起成长。
Lifecycle 的主要作用是简化实现生命周期感知型组件的复杂度。 在传统的方式中,需要手动从外部宿主(如 Activity、Fragment 或自定义宿主)中将生命周期事件分发到功能组件内部,这势必会造成宿主代码复杂度增加。例如:
MyActivity.kt
// Activity 宿主 class MyActivity : AppCompatActivity() { private val myWorker = MyWorker() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // 分发生命周期事件 myWorker.init() } override fun onStart(){ super.onStart() // 分发生命周期事件 myWorker.onStart() } override fun onStop() { super.onStop() // 分发生命周期事件 myWorker.onStop() } }
而使用 Lifecycle 组件后,能够将分发宿主生命周期事件的方法迁移到功能组件内部,宿主不再需要直接参与调整功能组件的生命周期。例如:
MyActivity.kt
// Activity 宿主 class MyActivity : AppCompatActivity() { private val myWorker = MyWorker() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // 注册观察者 lifecycle.addObserver(myWorker) } }
MyWorker.kt
class MyWorker : LifecycleEventObserver { override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) { // 分发生命周期事件 when (event) { Lifecycle.Event.ON_CREATE -> init() Lifecycle.Event.ON_START -> onStart() Lifecycle.Event.ON_STOP -> onStop() } } private fun init() { ... } private fun onStart() { ... } private fun onStop() { ... } }
Lifecycle 整体上采用了观察者模式,核心的 API 是 LifecycleObserver 和 LifecycleOwner:
LifecycleObserver.java
public interface LifecycleObserver { }
LifecycleOwner.java
public interface LifecycleOwner { @NonNull Lifecycle getLifecycle(); }
模块 build.gradle
// 过时方式(lifecycle-extensions 不再维护) implementation "androidx.lifecycle:lifecycle-extensions:2.4.0" // 目前的方式: def lifecycle_version = "2.5.0" // Lifecycle 核心类 implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version" // Lifecycle 注解处理器(用于处理 @OnLifecycleEvent 注解) kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version" // 应用进程级别 Lifecycle implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version"
注册观察者: Lifecycle 通过 addObserver(LifecycleObserver) 接口注册观察者,支持通过注解或非注解的方式注册观察者,共分为 3 种:
lifecycle.addObserver(object : LifecycleObserver { @OnLifecycleEvent(Lifecycle.Event.ON_CREATE) fun create() = {} @OnLifecycleEvent(Lifecycle.Event.ON_START) fun start() = {} @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) fun resume() = {} @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) fun pause() = {} @OnLifecycleEvent(Lifecycle.Event.ON_STOP) fun stop() = {} @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) fun destroy() = {} })
lifecycle.addObserver(object : LifecycleEventObserver { override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) { when (event) { ON_CREATE -> {} ON_START -> {} ON_RESUME -> {} ON_PAUSE -> {} ON_STOP -> {} ON_DESTROY -> {} ON_ANY -> {} } } })
// DefaultLifecycleObserver 是 FullLifecycleObserver 接口的空实现 lifecycle.addObserver(object : DefaultLifecycleObserver { override fun onCreate(owner: LifecycleOwner) {} override fun onStart(owner: LifecycleOwner) {} override fun onResume(owner: LifecycleOwner) {} override fun onPause(owner: LifecycleOwner) {} override fun onStop(owner: LifecycleOwner) {} override fun onDestroy(owner: LifecycleOwner) {} })
注意: Lifecycle 内部会禁止一个观察者注册到多个宿主上。这很好理解,要是绑定了多个宿主的话,Lifecycle 就不知道以哪个宿主的生命周期为准了。
目前,Android 预定义的 Lifecycle 宿主有 3 个:Activity、Fragment 和应用进程级别的宿主 ProcessLifecycleOwner:
前两个宿主大家都很熟悉了,第 3 个宿主 ProcessLifecycleOwner 则提供整个应用进程级别 Activity 的生命周期,能够支持非毫秒级别精度监听应用前后台切换的场景。
使用示例
ProcessLifecycleOwner.get().lifecycle.addObserver(object: LifecycleEventObserver{ override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) { ... } })
观察者必须绑定到宿主 LifecycleOwner 上,你可以使用系统预定义的宿主,或根据需要自定义宿主。主要步骤是实现 LifecycleOwner 并在内部将生命周期事件分发给调度器 LifecycleRegistry。模板如下:
LifecycleOwner.java
public interface LifecycleOwner { Lifecycle getLifecycle(); }
MyLifecycleOwner.kt
/** * 自定义宿主模板 */ class MyLifecycleOwner : LifecycleOwner { private val mLifecycleRegistry = LifecycleRegistry(this) override fun getLifecycle() = mLifecycleRegistry fun create() { // 并将生命周期状态分发给被观察者 mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE) } fun start() { // 并将生命周期状态分发给被观察者 mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START) } fun stop() { // 并将生命周期状态分发给被观察者 mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP) } ... }
Lifecycle#addObserver() 最终会分发到调度器 LifecycleRegistry 中,其中会将观察者和观察者持有的状态包装为一个节点,并且在注册时将观察者状态同步推进到与宿主相同的状态中。
LifecycleRegistry.java
private FastSafeIterableMap<LifecycleObserver, ObserverWithState> mObserverMap = new FastSafeIterableMap<>(); private State mState; @Override public void addObserver(LifecycleObserver observer) { // 观察者的初始状态:要么是 DESTROYED,要么是 INITIALIZED,确保观察者可以介绍到完整的事件流 State initialState = mState == DESTROYED ? DESTROYED : INITIALIZED; ObserverWithState statefulObserver = new ObserverWithState(observer, initialState); ObserverWithState previous = mObserverMap.putIfAbsent(observer, statefulObserver); ... // 将观察者推进到宿主最新的状态 State targetState = calculateTargetState(observer); while ((statefulObserver.mState.compareTo(targetState) < 0 && mObserverMap.contains(observer))) { pushParentState(statefulObserver.mState); statefulObserver.dispatchEvent(lifecycleOwner, upEvent(statefulObserver.mState)); popParentState(); // mState / subling may have been changed recalculate targetState = calculateTargetState(observer); } ... } @Override public void removeObserver(@NonNull LifecycleObserver observer) { mObserverMap.remove(observer); } // ObserverWithState:观察者及其观察状态 static class ObserverWithState { State mState; LifecycleEventObserver mLifecycleObserver; ObserverWithState(LifecycleObserver observer, State initialState) { // 用适配器包装观察者,实现对不同形式观察者的统一分发 mLifecycleObserver = Lifecycling.lifecycleEventObserver(observer); mState = initialState; } }
为了适配上面提到的不同类型的观察者,LifecycleRegistry 还为它们提供了一个适配层:非注解的方式会包装为一个 LifecycleEventObserver 的适配器对象,对于注解的方式,如果项目中引入了 annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
,会在编译时生成工具类 MyObserver_LifecycleAdapter
,否则会使用反射回调注解方法。
LifecycleRegistry.java
// ObserverWithState:观察者及其观察状态 static class ObserverWithState { State mState; // 适配器 LifecycleEventObserver mLifecycleObserver; ObserverWithState(LifecycleObserver observer, State initialState) { // 用适配器包装观察者,实现对不同形式观察者的统一分发 mLifecycleObserver = Lifecycling.lifecycleEventObserver(observer); mState = initialState; } void dispatchEvent(LifecycleOwner owner, Event event) { // 通过事件获得下一个状态 State newState = getStateAfter(event); mState = min(mState, newState); // 回调 onStateChanged() 方法 mLifecycleObserver.onStateChanged(owner, event); mState = newState; } }
Lifecycling.java
@NonNull static LifecycleEventObserver lifecycleEventObserver(Object object) { boolean isLifecycleEventObserver = object instanceof LifecycleEventObserver; boolean isFullLifecycleObserver = object instanceof FullLifecycleObserver; // 1. 观察者同时实现 LifecycleEventObserver 和 FullLifecycleObserver if (isLifecycleEventObserver && isFullLifecycleObserver) { return new FullLifecycleObserverAdapter((FullLifecycleObserver) object, (LifecycleEventObserver) object); } // 2. 观察者只实现 FullLifecycleObserver if (isFullLifecycleObserver) { return new FullLifecycleObserverAdapter((FullLifecycleObserver) object, null); } // 3. 观察者只实现 LifecycleEventObserver if (isLifecycleEventObserver) { return (LifecycleEventObserver) object; } // 4. 观察者使用注解方式: final Class<?> klass = object.getClass(); int type = getObserverConstructorType(klass); if (type == GENERATED_CALLBACK) { // APT 自动生成的 MyObserver_LifecycleAdapter List<Constructor<? extends GeneratedAdapter>> constructors = sClassToAdapters.get(klass); if (constructors.size() == 1) { GeneratedAdapter generatedAdapter = createGeneratedAdapter( constructors.get(0), object); return new SingleGeneratedAdapterObserver(generatedAdapter); } GeneratedAdapter[] adapters = new GeneratedAdapter[constructors.size()]; for (int i = 0; i < constructors.size(); i++) { adapters[i] = createGeneratedAdapter(constructors.get(i), object); } return new CompositeGeneratedAdaptersObserver(adapters); } // 反射调用 return new ReflectiveGenericLifecycleObserver(object); }
FullLifecycleObserverAdapter.java
class FullLifecycleObserverAdapter implements LifecycleEventObserver { private final FullLifecycleObserver mFullLifecycleObserver; private final LifecycleEventObserver mLifecycleEventObserver; FullLifecycleObserverAdapter(FullLifecycleObserver fullLifecycleObserver,LifecycleEventObserver lifecycleEventObserver) { mFullLifecycleObserver = fullLifecycleObserver; mLifecycleEventObserver = lifecycleEventObserver; } @Override public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) { // 分发到 mFullLifecycleObserver 和 mLifecycleEventObserver } }
宿主的生命周期事件需要分发到调度器 LifecycleRegistry 中,在高版本有直接观察 Activity 生命周期的 API,而在低版本使用无界面的 Fragment 间接观察 Activity 的生命周期。
androidx.activity.ComponentActivity.java
public class ComponentActivity extends androidx.core.app.ComponentActivity implements LifecycleOwner ...{ private final LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this); @NonNull @Override public Lifecycle getLifecycle() { return mLifecycleRegistry; } @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... ReportFragment.injectIfNeededIn(this); ... } }
ReportFragment.java
// 空白 Fragment public class ReportFragment extends Fragment { public static void injectIfNeededIn(Activity activity) { if (Build.VERSION.SDK_INT >= 29) { // 在高版本有直接观察 Activity 生命周期的 API activity.registerActivityLifecycleCallbacks(new LifecycleCallbacks()); } // 在低版本使用无界面的 Fragment 间接观察 Activity 的生命周期 android.app.FragmentManager manager = activity.getFragmentManager(); if (manager.findFragmentByTag(REPORT_FRAGMENT_TAG) == null) { manager.beginTransaction().add(new ReportFragment(), REPORT_FRAGMENT_TAG).commit(); // Hopefully, we are the first to make a transaction. manager.executePendingTransactions(); } } // 从 registerActivityLifecycleCallbacks() 或 Fragment 回调回来 static void dispatch(Activity activity, Lifecycle.Event event) { ... // 分发声明周期事件 activity.getLifecycle().handleLifecycleEvent(event); } }
当宿主的生命周期发生变化时,会分发到 LifecycleRegistry#handleLifecycleEvent(Lifecycle.Event)
,将观察者的状态回调到最新的状态上。
LifecycleRegistry.java
private FastSafeIterableMap<LifecycleObserver, ObserverWithState> mObserverMap =new FastSafeIterableMap<>(); private final WeakReference<LifecycleOwner> mLifecycleOwner; public LifecycleRegistry(@NonNull LifecycleOwner provider) { mLifecycleOwner = new WeakReference<>(provider); mState = INITIALIZED; } // 分发生命周期事件 public void handleLifecycleEvent(Lifecycle.Event event) { // 通过事件获得下一个状态 State next = getStateAfter(event); // 执行状态转移 moveToState(next); } private void moveToState(State next) { if (mState == next) { return; } mState = next; if (mHandlingEvent || mAddingObserverCounter != 0) { mNewEventOccurred = true; // we will figure out what to do on upper level. return; } mHandlingEvent = true; sync(); mHandlingEvent = false; } private void sync() { // isSynced() 判断所有观察者状态是否同步到最新状态 while (!isSynced()) { mNewEventOccurred = false; if (mState.compareTo(mObserverMap.eldest().getValue().mState) < 0) { // 生命周期回退,最终调用 ObserverWithState#dispatchEvent() 分发事件 backwardPass(lifecycleOwner); } Entry<LifecycleObserver, ObserverWithState> newest = mObserverMap.newest(); if (!mNewEventOccurred && newest != null && mState.compareTo(newest.getValue().mState) > 0) { // 生命周期前进,最终调用 ObserverWithState#dispatchEvent() 分发事件 forwardPass(lifecycleOwner); } } mNewEventOccurred = false; }
在 Activity 结束时,如果 Activity 上还存在未关闭的 Dialog,则会导致内存泄漏:
WindowLeaked: Activtiy MainActivity has leaked window DecorView@dfxxxx[MainActivity] thas was originally added here
解决方法:
BaseDialog.kt
class BaseDialog(context: Context) : Dialog(context), LifecycleEventObserver { init { if (context is ComponentActivity) { context.lifecycle.addObserver(this) } } override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) { if (Lifecycle.Event.ON_DESTROY == event) { if (isShowing) { dismiss() } } } }
Lifecycle 也加强了对 Kotlin 协程的支持 LifecycleCoroutineScope,我们可以构造出与生命周期相关联的协程作用域,主要支持 2 个特性:
使用示例
// 示例 1 lifecycleScope.launch { } // 示例 2(内部等价于示例 3) lifecycleScope.launchWhenResumed { } // 示例 3 lifecycleScope.launch { whenResumed { } }
1、自动取消协程实现原理分析: 核心在于 LifecycleCoroutineScopeImpl 中,内部在初始化时会注册一个观察者到宿主生命周期上,并在宿主进入 DESTROYED 时取消(cancel)协程。
LifecycleOwner.kt
// 基于 LifecycleOwner 的扩展函数 public val LifecycleOwner.lifecycleScope: LifecycleCoroutineScope get() = lifecycle.coroutineScope
Lifecycle.kt
public val Lifecycle.coroutineScope: LifecycleCoroutineScope get() { // 已简化 val newScope = LifecycleCoroutineScopeImpl( this, SupervisorJob() + Dispatchers.Main.immediate ) newScope.register() return newScope } public abstract class LifecycleCoroutineScope internal constructor() : CoroutineScope { internal abstract val lifecycle: Lifecycle ... // 开启协程再调用 whenResumed public fun launchWhenResumed(block: suspend CoroutineScope.() -> Unit): Job = launch { lifecycle.whenResumed(block) } } // 实现类 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() } } }
2、关联指定生命周期实现原理分析: 实现原理也是类似的,launchWhenResumed() 内部在 LifecycleContro 中注册观察者,最终通过协程调度器 PausingDispatcher
挂起(pause)或恢复(resume)协程。
PausingDispatcher.kt
public suspend fun <T> LifecycleOwner.whenResumed(block: suspend CoroutineScope.() -> T): T = lifecycle.whenResumed(block) public suspend fun <T> Lifecycle.whenResumed(block: suspend CoroutineScope.() -> T): T { return whenStateAtLeast(Lifecycle.State.RESUMED, block) } public suspend fun <T> Lifecycle.whenStateAtLeast( minState: Lifecycle.State, block: suspend CoroutineScope.() -> T ): T = withContext(Dispatchers.Main.immediate) { val job = coroutineContext[Job] ?: error("when[State] methods should have a parent job") // 分发器,内部持有一个分发队列,用于支持暂停协程 val dispatcher = PausingDispatcher() val controller = LifecycleController(this@whenStateAtLeast, minState, dispatcher.dispatchQueue, job) try { withContext(dispatcher, block) } finally { controller.finish() } }
LifecycleController.kt
@MainThread internal class LifecycleController( private val lifecycle: Lifecycle, private val minState: Lifecycle.State, private val dispatchQueue: DispatchQueue, parentJob: Job ) { private val observer = LifecycleEventObserver { source, _ -> // 分发宿主生命周期事件 if (source.lifecycle.currentState == Lifecycle.State.DESTROYED) { // 取消协程 parentJob.cancel() lifecycle.removeObserver(observer) dispatchQueue.finish() } else if (source.lifecycle.currentState < minState) { // 暂停协程 dispatchQueue.pause() } else { // 恢复协程 dispatchQueue.resume() } } init { // 直接取消协程 if (lifecycle.currentState == Lifecycle.State.DESTROYED) { // 取消协程 parentJob.cancel() lifecycle.removeObserver(observer) dispatchQueue.finish() } else { lifecycle.addObserver(observer) } } }
我们知道,Kotlin Flow 不具备生命周期感知的能力(当然了,Flow 是 Kotlin 生态的组件,不是仅针对 Android 生态的组件),那么 Flow 观察者如何保证在安全的生命周期订阅数据呢?
具体分析在 [4、Flow:LiveData 的替代方案]这篇文章里都讲过,这里不重复。
到这里,Jetpack 中最基础的 Lifecycle 组件就讲完了,下几篇文章我们将讨论基于 Lifecycle 实现的其他 Jetpack 组件,你知道是什么吗?关注我,带你了解更多。