1. Lifecycle 简介
Lifecycle 即生命周期,在Jetpack 架构组件中Lifecycles 是可以为其它组件提供生命周期感知能力的一个组件,因此把Lifecycle 也就生命周期感知组件,这个组件可以用来相应另一个组件(目前有Activity 和 Fragment)生命周期状态的变化,从而帮助我们写出更精简,更易于维护的代码。
2. 为什么需要 Lifecycle 组件 ?
Android App 大部分功能都是围绕着四大组件展开的,很多任务和操作都与四大组件中生命周期方法息息相关。
以Activity 和Fragment为例来说,需要在onCreate()方法回调的时候做什么? 在onResume()做什么? onStop(), onDestory()方法回调时又该做什么? 对于同一个Activity来说可能包含很多的业务逻辑,而组件的生命周期方法只有一个,这样就出现了一个问题,随着业务逻辑的越来越复杂,这些生命周期方法也会变得又重又难以维护。其实Activity 和 Fragment 作为四大组件中唯一的UI视图组件,本身只需要处理和用户的交互逻辑,其他逻辑都应该从其中隔离出来,做到视图和逻辑的分离,但实际上要做到这点并不容易,为了解决Activity或者生命周期方法会变得又重又难以维护的问题,很多人可能会考虑使用MVP架构来解决问题,这个在很大程度上确实能解决问题,让视图和逻辑分离,但其实本质的问题并未解决:随着业务逻辑的迭代和复杂,Presenter 会出现所谓的“接口爆炸”, 变得越来越难以维护和测试,并且很容易出现内存泄漏的情况。
3. Lifecycle 组件的使用?
3.1 依赖Lifecycle
Lifecycle 早期包含在 support library 26.1.0 及之后的依赖包中,如果我们的项目依赖的支持库版本在 26.1.0及以上,那么不需要额外导入 Lifecycle 库,本篇例子中使用的支持库是 28.0.0 :
implementation 'com.android.support:appcompat-v7:28.0.0'
如果项目依赖的support库小于26.1.0, 此时就需要单独依赖Lifecycle库了:
implementation "android.arch.lifecycle:runtime:1.1.1"
如果您的项目已经迁移到AndroidX, 使用下面方式引入:
implementation "androidx.lifecycle:lifecycle-runtime:2.0.0"
3.2 Lifecycle 两种使用方式:
3.2.1 使用系统Activity已经实现的LifecycleOwner
继承自AppCompatActivity,或者FragmentActivity, 可以直接使用实现自己的 LifecycleObserver ,然后addObserver即可。因为 AppCompatActivity,FragmentActivity都继承自ComponentActivity, 而 ComponentActivity已经实现了LifecycleOwner接口
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
lifecycle.addObserver(MyObserver())
}
class MyObserver : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun onMyCreate() {
Log.d("MainMyObserver", "onMyCreate")
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun onMyResume() {
Log.d("MainMyObserver", "onMyResume")
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onMyOnStop() {
Log.d("MainMyObserver", "onMyStop")
}
}
}
2020-07-04 11:11:46.651 15872-15872/com.cjl.jetpackdemo D/MyObserver: onMyCreate
2020-07-04 11:11:46.677 15872-15872/com.cjl.jetpackdemo D/MyObserver: onMyResume
2020-07-04 11:11:53.936 15872-15872/com.cjl.jetpackdemo D/MyObserver: onMyStop
3.2.2 自己实现 LifecycleOwner
class SecondActivity : Activity(), LifecycleOwner{
private lateinit var lifecycleRegistry : LifecycleRegistry
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.second_main)
lifecycleRegistry = LifecycleRegistry(this)
lifecycleRegistry.addObserver(MyObserver())
lifecycleRegistry.currentState = Lifecycle.State.CREATED
var state : Lifecycle.State = lifecycleRegistry.currentState
val isCreated = state.isAtLeast(Lifecycle.State.CREATED)
Log.d("SecondMyObserver", "isCreated = "+isCreated)
}
override fun onResume() {
super.onResume()
lifecycleRegistry.currentState = Lifecycle.State.RESUMED
}
override fun onDestroy() {
super.onDestroy()
lifecycleRegistry.currentState = Lifecycle.State.DESTROYED
}
override fun getLifecycle(): Lifecycle {
return lifecycleRegistry
}
class MyObserver : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun onMyCreate() {
Log.d("SecondMyObserver", "onMyCreate")
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun onMyResume() {
Log.d("SecondMyObserver", "onMyResume")
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onMyOnStop() {
Log.d("SecondMyObserver", "onMyStop")
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onMyOnDestroy() {
Log.d("SecondMyObserver", "onMyDestroy")
}
}
}
2020-07-04 19:15:26.635 21824-21824/com.cjl.jetpackdemo D/SecondMyObserver: onMyCreate
2020-07-04 19:15:26.636 21824-21824/com.cjl.jetpackdemo D/SecondMyObserver: isCreated = true
2020-07-04 19:15:26.642 21824-21824/com.cjl.jetpackdemo D/SecondMyObserver: onMyResume
2020-07-04 19:15:31.884 21824-21824/com.cjl.jetpackdemo D/SecondMyObserver: onMyStop
2020-07-04 19:15:31.884 21824-21824/com.cjl.jetpackdemo D/SecondMyObserver: onMyDestroy
为了更加方便使用,Lifecycle 还提供了查询当前组件所处的生命周期状态的方法, 源码如下:
public boolean isAtLeast(@NonNull State state) {
return compareTo(state) >= 0;
}
具体使用如下:
var state : Lifecycle.State = lifecycleRegistry.currentState
val isCreated = state.isAtLeast(Lifecycle.State.CREATED)
Log.d("SecondMyObserver", "isCreated = "+isCreated)
4. Lifecycle 的状态
Lifecycle 的状态定义在State枚举类中,代码具体如下:
public enum State {
DESTROYED,
INITIALIZED,
CREATED,
STARTED,
RESUMED;
public boolean isAtLeast(@NonNull State state) {
return compareTo(state) >= 0;
}
}
以上这五种状态似乎我们Activity的状态并不完全统一,没有Stop,多了个INITIALIZED, 下面我们就通过Demo来测试这五种状态触发的具体时机吧, Demo代码如下:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Log.d("MainMyObserver", "[onCreate start] lifecycle is---->"+lifecycle.currentState)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn_second.setOnClickListener { startActivity(Intent(this@MainActivity, SecondActivity::class.java)) }
lifecycle.addObserver(MyObserver())
Log.d("MainMyObserver", "[onCreate end] lifecycle is---->"+lifecycle.currentState)
}
override fun onStart() {
Log.d("MainMyObserver", "[onStart start] lifecycle is---->"+lifecycle.currentState)
super.onStart()
Log.d("MainMyObserver", "[onStart end] lifecycle is---->"+lifecycle.currentState)
}
override fun onResume() {
Log.d("MainMyObserver", "[onResume start] lifecycle is---->"+lifecycle.currentState)
super.onResume()
Log.d("MainMyObserver", "[onResume end] lifecycle is---->"+lifecycle.currentState)
}
override fun onPause() {
Log.d("MainMyObserver", "[onPause start] lifecycle is---->"+lifecycle.currentState)
super.onPause()
Log.d("MainMyObserver", "[onPause end] lifecycle is---->"+lifecycle.currentState)
}
override fun onStop() {
Log.d("MainMyObserver", "[onStop start] lifecycle is---->"+lifecycle.currentState)
super.onStop()
Log.d("MainMyObserver", "[onStop end] lifecycle is---->"+lifecycle.currentState)
}
override fun onDestroy() {
Log.d("MainMyObserver", "[onDestroy start] lifecycle is---->"+lifecycle.currentState)
super.onDestroy()
Log.d("MainMyObserver", "[onDestroy end] lifecycle is---->"+lifecycle.currentState)
}
class MyObserver : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun onMyCreate() {
Log.d("MainMyObserver", "onMyCreate")
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onMyStart() {
Log.d("MainMyObserver", "onMyStart")
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun onMyResume() {
Log.d("MainMyObserver", "onMyResume")
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun onMyPause() {
Log.d("MainMyObserver", "onMyPause")
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onMyOnStop() {
Log.d("MainMyObserver", "onMyStop")
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onMyOnDestroy() {
Log.d("MainMyObserver", "onMyDestroy")
}
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
fun onMyAny() {
// Log.d("MainMyObserver", "onMyAny")
}
}
}
测试后log如下:
2020-07-04 19:42:37.290 28018-28018/com.cjl.jetpackdemo D/MainMyObserver: [onCreate start] lifecycle is---->INITIALIZED
2020-07-04 19:42:37.340 28018-28018/com.cjl.jetpackdemo D/MainMyObserver: [onCreate end] lifecycle is---->INITIALIZED
2020-07-04 19:42:37.341 28018-28018/com.cjl.jetpackdemo D/MainMyObserver: onMyCreate
2020-07-04 19:42:37.345 28018-28018/com.cjl.jetpackdemo D/MainMyObserver: [onStart start] lifecycle is---->CREATED
2020-07-04 19:42:37.346 28018-28018/com.cjl.jetpackdemo D/MainMyObserver: [onStart end] lifecycle is---->CREATED
2020-07-04 19:42:37.346 28018-28018/com.cjl.jetpackdemo D/MainMyObserver: onMyStart
2020-07-04 19:42:37.347 28018-28018/com.cjl.jetpackdemo D/MainMyObserver: [onResume start] lifecycle is---->STARTED
2020-07-04 19:42:37.348 28018-28018/com.cjl.jetpackdemo D/MainMyObserver: [onResume end] lifecycle is---->STARTED
2020-07-04 19:42:37.348 28018-28018/com.cjl.jetpackdemo D/MainMyObserver: onMyResume
2020-07-04 19:42:44.705 28018-28018/com.cjl.jetpackdemo D/MainMyObserver: onMyPause
2020-07-04 19:42:44.705 28018-28018/com.cjl.jetpackdemo D/MainMyObserver: [onPause start] lifecycle is---->STARTED
2020-07-04 19:42:44.706 28018-28018/com.cjl.jetpackdemo D/MainMyObserver: [onPause end] lifecycle is---->STARTED
2020-07-04 19:42:45.099 28018-28018/com.cjl.jetpackdemo D/MainMyObserver: onMyStop
2020-07-04 19:42:45.100 28018-28018/com.cjl.jetpackdemo D/MainMyObserver: [onStop start] lifecycle is---->CREATED
2020-07-04 19:42:45.101 28018-28018/com.cjl.jetpackdemo D/MainMyObserver: [onStop end] lifecycle is---->CREATED
2020-07-04 19:42:45.104 28018-28018/com.cjl.jetpackdemo D/MainMyObserver: onMyDestroy
2020-07-04 19:42:45.105 28018-28018/com.cjl.jetpackdemo D/MainMyObserver: [onDestroy start] lifecycle is---->DESTROYED
2020-07-04 19:42:45.106 28018-28018/com.cjl.jetpackdemo D/MainMyObserver: [onDestroy end] lifecycle is---->DESTROYED
从log中我们可以看出以下几点结论:
- Lifecycle 中的回调方法的调用有些是在Activity 生命周期方法执行完后才回调的,有些是在Activity生命周期方法执行之前回调的,具体来说Lifecycle的 onCreate, onStart, onResume 方法是在Activity生命周期调用之后才回调的; 而Lifecycle的 onPause, onStop, onDestroy是在Activity生命周期方法执行之前就先回调的;
- 在Activity 的 onCreate 方法中,Lifecycle 的状态一直INITIALIZED;
- 在Activity 的 onStart 方法中,Lifecycle 的状态一直CREATED;
- 在Activity 的 onResume 方法中,Lifecycle 的状态一直STARTED;
- 在Activity 的 onPause 即将调用之前,Lifecycle 的状态仍然是STARTED;
- 在Activity 的 onStop 即将调用之前,Lifecycle 的状态仍然是CREATED;
- 在Activity 的 onDestroy 即将调用之前,Lifecycle 的状态已经是DESTROYED了;
以上就是Lifecycle的基本使用方法,可以看出其实很简单,赶紧用在项目中吧。下篇我们去看源码,探究具体是如何实现的。