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()
}
}
打印结果:
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)
}
}
打印结果:
6). 组件原理
7). 时序图
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). 代码下载

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
Android EventBus使用(不含源码解析)
官方文档:https://github.com/greenrobot/EventBus simplifies the communication between components decouples event senders and receivers performs well with Activities, Fragments, and background threads avoids complex and error-prone dependencies and life cycle issues makes your code simpler is fast is tiny (~50k jar) is proven in practice by apps with 100,000,000+ installs has advanced features like delivery threads, subscriber priorities, etc. 这句话大概是说: 简化组件之间的通信 解耦事件发送者和接收者 对活动、片段和后台线程进行良好的操作 而且非常快 ja...
-
下一篇
动画必须有(二):悬浮菜单了解一下!
动画必须有(一): 属性动画浅谈githhub传送门 目录 前言 效果图 FloatingActionButton基础 FloatingActionButton实例 最后 前言 悬浮按钮是我非常喜欢的, 可以把最关键的功能放入到悬浮按钮中. 比如日记app里的新建日记, 阅读类app里的喜欢. 稍微处理一下可以将悬浮按钮扩展成悬浮菜单, 来看下实现吧! github直接看源码 效果图 废话不多说, 先看图, 感兴趣再往下看! 悬浮菜单 FloatingActionButton基础 记得导包. compile 'com.android.support:design:26.+' 可以看看谷歌官方介绍. 你会被瞬间圈粉. 然后是官方文档, 这个文档说了如何调用. 搭配Snackbar 官方推荐配合Snackbar来使用, 这都不多说了. 配合Snackbar 显示和隐藏 然后还有就是悬浮按钮的隐藏和显示函数. Button btHide = (Button) findViewById(R.id.bt_hide); btHide.setOnClickListener(new View.OnCl...
相关文章
文章评论
共有0条评论来说两句吧...