Lifecycle:管理并监听Activity生命周期
【1】依赖
def lifecycle_version = "2.2.0"
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-viewmodel�savedstate:$lifecycle_version"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-service:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version"
【2】完整实现步骤
第一步:将 Activity 做为被观察者
class LifecycleActivity : AppCompatActivity(), LifecycleOwner {
// 注册类
private val lifecycle : LifecycleRegistry = LifecycleRegistry(this@LifecycleActivity)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
....
}
// 重写 Lifecycle 回调方法
override fun getLifecycle(): Lifecycle {
return super.getLifecycle()
}
}
即,实现 LifecycleOwner 接口。
第二步:构建观察者
class MyObserver : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun onResume() {
Log.d("MyObserver", "Lifecycle call onResume")
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun onPause() {
Log.d("MyObserver", "Lifecycle call onPause")
}
}
上面的两个注解中的枚举对应了两个不同的生命周期,所有的枚举如下:
ON_CREATE,
ON_START,
ON_RESUME,
ON_PAUSE,
ON_STOP,
ON_DESTROY,
ON_ANY
使用 OnLifecycleEvent 注解指定指定生命周期的回调方法。
第三步:添加观察者(订阅)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
getLifecycle().addObserver(MyObserver())
// 使用上面定义的 LifecycleRegistry 对象也可以
lifecycle.addObserver(MyObserver())
}
第四步:调试生命周期和监听回调方法的执行顺序。
【3】简单实现方式
上面完整的实现方式是可以实现的更加简单,原因是当前Activity父类或者间接父类已经完成了一部分。
完整代码如下:
class LifecycleActivity : AppCompatActivity(), LifecycleOwner {
private lateinit var binding : ActivityLifecycleBinding
private val lifecycle : LifecycleRegistry = LifecycleRegistry(this@LifecycleActivity)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView<ActivityLifecycleBinding>(this@LifecycleActivity, R.layout.activity_lifecycle)
getLifecycle().addObserver(MyObserver())
// 或者
lifecycle.addObserver(MyObserver())
}
override fun getLifecycle(): Lifecycle {
return super.getLifecycle()
}
// ...
// MyObserver 和 生命周期方法省略
}
第一步:省略 LifecycleOwner 接口的实现,因为 LifecycleActivity 的间接父类已经实现了 LifecycleOwner 接口。
LifecycleOwner 接口的实现方法 getLifecycle 也会被删除。
class LifecycleActivity : AppCompatActivity() {
private lateinit var binding : ActivityLifecycleBinding
private val lifecycle : LifecycleRegistry = LifecycleRegistry(this@LifecycleActivity)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView<ActivityLifecycleBinding>(this@LifecycleActivity, R.layout.activity_lifecycle)
getLifecycle().addObserver(MyObserver())
// 或者
lifecycle.addObserver(MyObserver())
}
// ...
// MyObserver 和 生命周期方法省略
}
第二步:在 ComponentActivity 中, 已经创建了 LifecycleRegistry 实例,
如果您的Activity实现了 ComponentActivity,那么可以省略 LifecycleRegistry 实例的创建。
class LifecycleActivity : AppCompatActivity() {
private lateinit var binding : ActivityLifecycleBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView<ActivityLifecycleBinding>(this@LifecycleActivity, R.layout.activity_lifecycle)
getLifecycle().addObserver(MyObserver())
}
// ...
// MyObserver 和 生命周期方法省略
}
如果是Kotlin, 还可以将:
getLifecycle().addObserver(MyObserver())
写成:
lifecycle.addObserver(MyObserver())
最终,简化之后的完成代码如下:
class LifecycleActivity : AppCompatActivity() {
private lateinit var binding : ActivityLifecycleBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView<ActivityLifecycleBinding>(this@LifecycleActivity, R.layout.activity_lifecycle)
lifecycle.addObserver(MyObserver())
}
override fun onResume() {
Log.d("MyObserver", "before onResume")
super.onResume()
Log.d("MyObserver", "after onResume")
}
override fun onPause() {
Log.d("MyObserver", "before onPause")
super.onPause()
Log.d("MyObserver", "after onPause")
}
class MyObserver : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun onResume() {
Log.d("MyObserver", "Lifecycle call onResume")
}
@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
fun onPause() {
Log.d("MyObserver", "Lifecycle call onPause")
}
}
}
【4】另一种实现方式
优雅的管理Activity - 简书 (jianshu.com)
[本章完...]
网友评论