1). 简介
生命周期感知组件执行操作以响应另一个组件(例如活动和片段)的生命周期状态的更改。 这些组件可帮助您生成更易于组织且通常更轻量级的代码,这些代码更易于维护。
一种常见的模式是在活动和片段的生命周期方法中实现依赖组件的操作。 但是,这种模式导致代码组织不良以及错误的增加。 通过使用生命周期感知组件,您可以将依赖组件的代码移出生命周期方法并移入组件本身。
2). 依赖
// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:$lifecycle_version"
// alternatively - just ViewModel
implementation "android.arch.lifecycle:viewmodel:$lifecycle_version" // use -ktx for Kotlin
// alternatively - just LiveData
implementation "android.arch.lifecycle:livedata:$lifecycle_version"
// alternatively - Lifecycles only (no ViewModel or LiveData).
// Support library depends on this lightweight import
implementation "android.arch.lifecycle:runtime:$lifecycle_version"
annotationProcessor "android.arch.lifecycle:compiler:$lifecycle_version" // use kapt for Kotlin
// alternately - if using Java8, use the following instead of compiler
implementation "android.arch.lifecycle:common-java8:$lifecycle_version"
// optional - ReactiveStreams support for LiveData
implementation "android.arch.lifecycle:reactivestreams:$lifecycle_version"
// optional - Test helpers for LiveData
testImplementation "android.arch.core:core-testing:$lifecycle_version"
3). 解决问题
- 主要解决MVP模式时Activity和Fragment声明周期问题
4). 原MVP写法
- Presenter接口
interface IPresenter {
fun onCreate()
fun onStart()
fun onResume()
fun onPause()
fun onStop()
fun onDestroy()
}
- Presenter实现
class CustomPresenter : IPresenter {
companion object {
private val TAG = CustomPresenter::class.java.simpleName
}
override fun onCreate() {
Log.e(TAG, "-- onCreate")
}
override fun onStart() {
Log.e(TAG, "-- onStart")
}
override fun onResume() {
Log.e(TAG, "-- onResume")
}
override fun onPause() {
Log.e(TAG,"-- onPause")
}
override fun onStop() {
Log.e(TAG, "-- onStop")
}
override fun onDestroy() {
Log.e(TAG, "-- onDestroy")
}
}
- Activity代码
class CustomLifeCycleActivity : AppCompatActivity() {
private lateinit var presenter: IPresenter
companion object {
private val TAG = CustomLifeCycleActivity::class.java.simpleName
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_custom_life_cyclle)
presenter = CustomPresenter()
Log.e(TAG, "-- onCreate")
presenter.onCreate()
}
override fun onStart() {
super.onStart()
Log.e(TAG, "-- onStart")
presenter.onStart()
}
override fun onResume() {
super.onResume()
Log.e(TAG, "-- onResume")
presenter.onResume()
}
override fun onPause() {
super.onPause()
Log.e(TAG, "-- onPause")
presenter.onPause()
}
override fun onStop() {
super.onStop()
Log.e(TAG, "-- onStop")
presenter.onStop()
}
override fun onDestroy() {
super.onDestroy()
Log.e(TAG, "-- onDestroy")
presenter.onDestroy()
}
}
打印结果:
图1.png
5). Lifecycle写法
- Presenter接口
interface IPresenter : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun onCreate(@NotNull owner: LifecycleOwner)
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy(@NotNull owner: LifecycleOwner)
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
fun onLifecycleChanged(@NotNull owner: LifecycleOwner)
}
- Presenter实现
class SpecialPresenter : IPresenter {
companion object {
private val TAG = SpecialPresenter::class.java.simpleName
}
override fun onCreate(owner: LifecycleOwner) {
Log.e(TAG, "onCreate")
}
override fun onDestroy(owner: LifecycleOwner) {
Log.e(TAG, "onDestroy")
}
override fun onLifecycleChanged(owner: LifecycleOwner) {
Log.e(TAG, "onLifecycleChanged")
}
}
- Activity代码
class SpecialLifeCycleActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_special_life_cycle)
val presenter = SpecialPresenter()
// 添加观察者
lifecycle.addObserver(presenter)
}
}
打印结果:
图2.png
6). 组件原理
原理图.png7). 时序图
时序图.png8). Event枚举
public enum Event {
/**
* Constant for onCreate event of the {@link LifecycleOwner}.
*/
ON_CREATE,
/**
* Constant for onStart event of the {@link LifecycleOwner}.
*/
ON_START,
/**
* Constant for onResume event of the {@link LifecycleOwner}.
*/
ON_RESUME,
/**
* Constant for onPause event of the {@link LifecycleOwner}.
*/
ON_PAUSE,
/**
* Constant for onStop event of the {@link LifecycleOwner}.
*/
ON_STOP,
/**
* Constant for onDestroy event of the {@link LifecycleOwner}.
*/
ON_DESTROY,
/**
* An {@link Event Event} constant that can be used to match all events.
*/
ON_ANY
}
网友评论