Architecture -- Lifecycle

简介: 1). 简介生命周期感知组件执行操作以响应另一个组件(例如活动和片段)的生命周期状态的更改。 这些组件可帮助您生成更易于组织且通常更轻量级的代码,这些代码更易于维护。
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()
  }
}

打印结果:


img_0f3c1e1c8b0bc986f43d5a62dfdbadd2.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)
  }
}

打印结果:


img_1c78dd2fd154a51c2db74dfcce72a141.png
图2.png
6). 组件原理
img_79ec8d1dad7606d6fbf8c9d4156bb8d8.png
原理图.png
7). 时序图
img_2190a4d19405a6c9e702d809575a2dab.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). 代码下载
目录
相关文章
|
开发工具
Flutter ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
Flutter ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
298 0
|
Serverless Python
Deploy a flexible and highly available image processing service within 10 minutes
Alibaba Cloud Function Compute is an event-driven and fully-managed compute service. With Function Compute, you can quickly build any type of applicat.
1487 0
|
Android开发 Kotlin Java
Architecture -- LiveData
1). 简介 LiveData是一个可观察的数据持有者类。 与常规observable不同,LiveData是生命周期感知的,这意味着它尊重其他应用程序组件的生命周期,例如活动,片段或服务。
1045 0
|
数据库 数据库管理 缓存
Architecture -- Room
1). 简介 Room persistence库为SQLite提供了一个抽象层,以便在利用SQLite的全部功能的同时实现更强大的数据库访问。 该库可帮助您在运行应用程序的设备上创建应用程序数据的缓存。
892 0
|
Android开发 Java Kotlin
Architecture -- WorkManager
1. WorkManager 1). 简介 其实就是"管理一些要在后台工作的任务, -- 即使你的应用没启动也能保证任务能被执行",WorkManager在底层, 会根据你的设备情况, 选用JobScheduler, Firebase的JobDispatcher, 或是AlarmManager。
904 0