Architecture -- Lifecycle

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()
  }
}

打印结果:


3110861-1b13f6c505b808a2.png
图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)
  }
}

打印结果:


3110861-d2ba0251bb72e4db.png
图2.png
6). 组件原理
3110861-c1b525c68bd57011.png
原理图.png
7). 时序图
3110861-f1d5e5c9c7bf23c5.png
时序图.png
8). 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
    }
9). 原文链接
10). 代码下载
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值