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). 代码下载
目录
相关文章
|
数据挖掘
SAP QM 在Quality Notification里维护Internal Note
SAP QM 在Quality Notification里维护Internal Note
SAP QM 在Quality Notification里维护Internal Note
How is note created - SAP backend implementation
How is note created - SAP backend implementation
102 0
How is note created - SAP backend implementation
How is note created - backend implementation
Created by Wang, Jerry, last modified on Jan 15, 2015
103 0
How is note created - backend implementation
|
Web App开发 JavaScript 前端开发
Fiori as a Service - FaaS - Creation of inline task option is not available
Fiori as a Service - FaaS - Creation of inline task option is not available
Fiori as a Service - FaaS - Creation of inline task option is not available
SAP Fiori extension hook added via note
SAP Fiori extension hook added via note
SAP Fiori extension hook added via note
SAP CRM Application Extension Tool的Custom Behavior
SAP CRM Application Extension Tool的Custom Behavior
116 0
SAP CRM Application Extension Tool的Custom Behavior
Access the OData service exposed by your CDS view via Smart Business Tile
It is possible to configure a Smart business tile which consumes the OData service exposed by your CDS view. Create a new KPI definition:
100 0
Access the OData service exposed by your CDS view via Smart Business Tile
filter operation implementation in SAP Gateway framework
Created by Wang, Jerry, last modified on Dec 30, 2015
filter operation implementation in SAP Gateway framework
|
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.
1441 0