前言
最近在学习 Google 推出的框架Jetpack,虽然目前网上的资料已经很多了,但为了加深印象和边动手练习跟着学习,所以站在了下面的巨人的肩膀上,并根据当前最新的API 和编写实际Demo,记录下一些学习笔记,大部分是参考巨人们的,整理和休整,加入自己理解和更新吧,学习领略了Android Jetpack组件的一点魅力
目前学习笔记系列为:
- Android Jetpack 之 Lifecycles ---入门使用
- Android Jetpack 之 LiveData---入门使用
- Android Jetpack 之 Room ---入门使用
- Android Jetpack 之 WorkManger---入门使用
- ....待续
日常鸣谢巨人
正题
Lifecycle 简介
Lifecycle 是一个持有组件生命周期信息的类,并允许其他对象观察此状态
生命周期使用两个主要枚举来跟踪其关联组件的生命周期状态
Event - 从框架和Lifecycle 类派发的生命周期事件, 这些事件映射到activity 和fragment 中的回调事件
State - 由 Lifecycle 对象跟踪的组件的当前状态
Lifecycles 使用场景
生命周期感知组件,从名字中就可以知道,主要是用来是处理生命周期的相关的操作
在平时的开发过程中,我们难免有些逻辑的执行是和UI的生命周期相结合的,需要在特定的生命周期中执行相应的方法,我们平时做的可能就是在View中的每个周期调用Present中获取数据的方法,然后在调用View的回调接口更新UI
但现在使用Lifecycles可以使用注解和观察的模式自动调用Observe中定义好的方法,也就是 Android Jetpack 中的 Lifecycles
Lifecycles 的基本使用步骤
1. gradle的引入(以Kotlin语言为例)
Project -> build.gradle
repositories {
google()
}
Module -> build.gradle
def lifecycle_version = "2.0.0"
// alternatively - Lifecycles only (no ViewModel or LiveData). Some UI
// AndroidX libraries use this lightweight import for Lifecycle
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
// For Kotlin use kapt instead of annotationProcessor
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"
// alternately - if using Java8, use the following instead of lifecycle-compiler
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
2. 创建MyObserver
class MyObserver(var lifecycle: Lifecycle, var myLifecyckleCallback: MyLifecyckleCallback) : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
public fun connectOnCreate() {
println("OnCreate")
}
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public fun connectOnResume() {
println("OnResume")
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public fun connectOnStart() {
println("OnStart")
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public fun connectOnDestory() {
println("OnDestory")
}
fun println(string: String) {
myLifecyckleCallback.update(string)
}
}
3. 创建Activity并实现LifecyclesOwner
class MyLifeCycleActivity : AppCompatActivity() {
private lateinit var lifecycleRegistry: LifecycleRegistry
private lateinit var myObserver: MyObserver
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
lifecycleRegistry = LifecycleRegistry(this)
lifecycleRegistry.markState(Lifecycle.State.CREATED)
myObserver = MyObserver(lifecycleRegistry, object : MyLifecyckleCallback {
override fun update(string: String) {
Toast.makeText(this@MyLifeCycleActivity, string, Toast.LENGTH_SHORT).show()
}
})
lifecycleRegistry.addObserver(myObserver)
}
override fun onStart() {
super.onStart()
lifecycleRegistry.markState(Lifecycle.State.STARTED)
}
override fun onResume() {
super.onResume()
lifecycleRegistry.markState(Lifecycle.State.RESUMED)
}
override fun onStop() {
super.onStop()
lifecycleRegistry.markState(Lifecycle.State.DESTROYED)
}
override fun onDestroy() {
super.onDestroy()
lifecycleRegistry.markState(Lifecycle.State.DESTROYED)
}
override fun getLifecycle(): Lifecycle {
return lifecycleRegistry
}
}
因为26.1.0 高版本后的 Activity 已经实现了LifecycleOwner 接口
所以,这里不用再实现该接口
如果是有其他类需要实现这样的功能,需要实现 LifecycleOwner 接口,类似如下
class MyActivity : Activity(), LifecycleOwner {
private lateinit var lifecycleRegistry: LifecycleRegistry
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
lifecycleRegistry = LifecycleRegistry(this)
lifecycleRegistry.markState(Lifecycle.State.CREATED)
}
public override fun onStart() {
super.onStart()
lifecycleRegistry.markState(Lifecycle.State.STARTED)
}
override fun getLifecycle(): Lifecycle {
return lifecycleRegistry
}
}
4. 创建LifecycleRegistry,如果实现了已经实现了LifecycleOwner 接口,并返回lifecycleRegistry
private lateinit var lifecycleRegistry: LifecycleRegistry
lifecycleRegistry = LifecycleRegistry(this)
override fun getLifecycle(): Lifecycle {
return lifecycleRegistry
}
5. 实例化创建MyObserver,绑定Lifecycles,并绑定 Lifecycle
private lateinit var myObserver: MyObserver
myObserver = MyObserver(lifecycleRegistry, object : MyLifecyckleCallback {
override fun update(string: String) {
Toast.makeText(this@MyLifeCycleActivity, string, Toast.LENGTH_SHORT).show()
}
})
lifecycleRegistry.addObserver(myObserver)
6. 在生命周期中设置相应的标记
lifecycleRegistry.markState(Lifecycle.State.STARTED)
....
lifecycleRegistry.markState(Lifecycle.State.RESUMED)
7. 上述辅助接口
interface MyLifecyckleCallback {
fun update(string: String)
}
内部概要实现过程
系统提供了Activity每个生命周期对应的Event,而Event有会有对应的State
//生命周期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
}
//5个对应的生命周期状态
public enum State {
DESTROYED,
INITIALIZED,
CREATED,
STARTED,
RESUMED;
public boolean isAtLeast(@NonNull State state) {
return compareTo(state) >= 0;
}
}
- 使用 OnLifecycleEvent 标记每个执行方法的Event
- 当活动状态改变时,系统会判断即将要改变成的状态
- 根据状态获取要执行的Event
- 从注册的Observer中获取标注为对应的Event,执行逻辑