用于帮助开发者管理Activity和Fragment 的生命周期,它是LiveData和ViewModel的基础。
备注:
- appcompat依赖androidx.fragment
- androidx.fragment依赖ViewModel和 LiveData
- LiveData依赖Lifecycle
问题:
activity的onStart / onStop 中,开启 / 关闭 功能组件
1.大量功能组件难以维护
2.无法保证在activity停止后(比如做一个耗时异步的操作后,再开启组建),这些组件一定会停止,从而有可能导致内存泄露
基本使用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_lifecycle) lifecycle.addObserver(MyObserver()) }
override fun onStart() { super.onStart() Log.d(TAG, "onStart: " + lifecycle.currentState) } override fun onResume() { super.onResume() Log.d(TAG, "onResume: " + lifecycle.currentState) } override fun onPause() { super.onPause() Log.d(TAG, "onPause: " + lifecycle.currentState) } override fun onStop() { super.onStop() Log.d(TAG, "onStop: " + lifecycle.currentState) } override fun onDestroy() { super.onDestroy() Log.d(TAG, "onDestroy: " + lifecycle.currentState) }
class MyObserver : LifecycleObserver {
@OnLifecycleEvent(value = Lifecycle.Event.ON_CREATE) fun fun1(owner: LifecycleOwner) { Log.d("TestLifecycleActivity", "ON_CREATE") } @OnLifecycleEvent(value = Lifecycle.Event.ON_START) fun fun0(owner: LifecycleOwner) { Log.d("TestLifecycleActivity", "ON_START") } @OnLifecycleEvent(value = Lifecycle.Event.ON_RESUME) fun fun2(owner: LifecycleOwner) { Log.d("TestLifecycleActivity", "ON_RESUME") } @OnLifecycleEvent(value = Lifecycle.Event.ON_PAUSE) fun fun3() { Log.d("TestLifecycleActivity", "ON_PAUSE") } @OnLifecycleEvent(value = Lifecycle.Event.ON_STOP) fun fun4() { Log.d("TestLifecycleActivity", "ON_STOP") } @OnLifecycleEvent(value = Lifecycle.Event.ON_DESTROY) fun fun5() { Log.d("TestLifecycleActivity", "ON_DESTROY") }
}
TestLifecycleActivity: onCreate: INITIALIZED TestLifecycleActivity: ON_CREATE TestLifecycleActivity: onStart: CREATED TestLifecycleActivity: ON_START TestLifecycleActivity: onResume: STARTED TestLifecycleActivity: ON_RESUME
TestLifecycleActivity: ON_PAUSE TestLifecycleActivity: onPause: STARTED TestLifecycleActivity: ON_STOP TestLifecycleActivity: onStop: CREATED TestLifecycleActivity: ON_DESTROY TestLifecycleActivity: onDestroy: DESTROYED
|
具体运用:
1.功能组件 实现 LifecycleObserver 接口
2.在@OnLifecycleEvent(value = Lifecycle.Event.ON_START)中实现开启功能,开启前还需检查当前状态:
1 2 3
| if(owner.getLifecycle().getCurrentState().isAtLeast(STARTED)){ startSomething(); }
|
3.在@OnLifecycleEvent(value = Lifecycle.Event.ON_STOP)中实现关闭功能
- 功能组件自动感知生命周期,组件处理自己逻辑,减轻Activity/Fragment中代码,易于管理
- 检查当前状态,至少处于STARTED状态才会继续执行start()
以上两点解决上述问题1、2
Application生命周期 ProcessLifecycleOwner
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| class MyApp : Application() {
override fun onCreate() { super.onCreate() INSTANCE = this ProcessLifecycleOwner.get().lifecycle.addObserver(MyAppLifeCycleObserver()) }
class MyAppLifeCycleObserver:LifecycleObserver{ @OnLifecycleEvent(Lifecycle.Event.ON_START) fun onAppForeground() { Log.d("MyApp", "ON_START -- onAppForeground") }
@OnLifecycleEvent(Lifecycle.Event.ON_STOP) fun onAppBackground() { Log.d("MyApp", "ON_STOP -- onAppBackground") }
}
}
MyApp: ON_START -- onAppForeground MyApp: ON_STOP -- onAppBackground
|
Lifecycle.Event.ON_CREATE只会分发一次,Lifecycle.Event.ON_DESTROY不会被分发。
第一个Activity进入时,ProcessLifecycleOwner将分派Lifecycle.Event.ON_START, Lifecycle.Event.ON_RESUME。而Lifecycle.Event.ON_PAUSE, Lifecycle.Event.ON_STOP,将在最后一个Activit退出后后延迟分发。
参考链接:
https://juejin.cn/post/6893870636733890574#comment
https://juejin.cn/post/6844904111108800519