前言
最近在學(xué)習(xí) Google 推出的框架Jetpack巢钓,雖然目前網(wǎng)上的資料已經(jīng)很多了瀑焦,但為了加深印象和邊動(dòng)手練習(xí)跟著學(xué)習(xí),所以站在了下面的巨人的肩膀上,并根據(jù)當(dāng)前最新的API 和編寫實(shí)際Demo,記錄下一些學(xué)習(xí)筆記,大部分是參考巨人們的裆甩,整理和休整,加入自己理解和更新吧齐唆,學(xué)習(xí)領(lǐng)略了Android Jetpack組件的一點(diǎn)魅力
目前學(xué)習(xí)筆記系列為:
- Android Jetpack 之 Lifecycles ---入門使用
- Android Jetpack 之 LiveData---入門使用
- Android Jetpack 之 Room ---入門使用
- Android Jetpack 之 WorkManger---入門使用
- ....待續(xù)
日常鳴謝巨人
正題
Lifecycle 簡(jiǎn)介
Lifecycle 是一個(gè)持有組件生命周期信息的類嗤栓,并允許其他對(duì)象觀察此狀態(tài)
生命周期使用兩個(gè)主要枚舉來跟蹤其關(guān)聯(lián)組件的生命周期狀態(tài)
Event - 從框架和Lifecycle 類派發(fā)的生命周期事件, 這些事件映射到activity 和fragment 中的回調(diào)事件
State - 由 Lifecycle 對(duì)象跟蹤的組件的當(dāng)前狀態(tài)
Lifecycles 使用場(chǎng)景
生命周期感知組件箍邮,從名字中就可以知道茉帅,主要是用來是處理生命周期的相關(guān)的操作
在平時(shí)的開發(fā)過程中,我們難免有些邏輯的執(zhí)行是和UI的生命周期相結(jié)合的锭弊,需要在特定的生命周期中執(zhí)行相應(yīng)的方法堪澎,我們平時(shí)做的可能就是在View中的每個(gè)周期調(diào)用Present中獲取數(shù)據(jù)的方法,然后在調(diào)用View的回調(diào)接口更新UI
但現(xiàn)在使用Lifecycles可以使用注解和觀察的模式自動(dòng)調(diào)用Observe中定義好的方法味滞,也就是 Android Jetpack 中的 Lifecycles
Lifecycles 的基本使用步驟
1. gradle的引入(以Kotlin語(yǔ)言為例)
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. 創(chuàng)建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. 創(chuàng)建Activity并實(shí)現(xiàn)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
}
}
因?yàn)?6.1.0 高版本后的 Activity 已經(jīng)實(shí)現(xiàn)了LifecycleOwner 接口
所以樱蛤,這里不用再實(shí)現(xiàn)該接口
如果是有其他類需要實(shí)現(xiàn)這樣的功能,需要實(shí)現(xiàn) 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. 創(chuàng)建LifecycleRegistry昨凡,如果實(shí)現(xiàn)了已經(jīng)實(shí)現(xiàn)了LifecycleOwner 接口,并返回lifecycleRegistry
private lateinit var lifecycleRegistry: LifecycleRegistry
lifecycleRegistry = LifecycleRegistry(this)
override fun getLifecycle(): Lifecycle {
return lifecycleRegistry
}
5. 實(shí)例化創(chuàng)建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. 在生命周期中設(shè)置相應(yīng)的標(biāo)記
lifecycleRegistry.markState(Lifecycle.State.STARTED)
....
lifecycleRegistry.markState(Lifecycle.State.RESUMED)
7. 上述輔助接口
interface MyLifecyckleCallback {
fun update(string: String)
}
內(nèi)部概要實(shí)現(xiàn)過程
系統(tǒng)提供了Activity每個(gè)生命周期對(duì)應(yīng)的Event,而Event有會(huì)有對(duì)應(yīng)的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個(gè)對(duì)應(yīng)的生命周期狀態(tài)
public enum State {
DESTROYED,
INITIALIZED,
CREATED,
STARTED,
RESUMED;
public boolean isAtLeast(@NonNull State state) {
return compareTo(state) >= 0;
}
}
- 使用 OnLifecycleEvent 標(biāo)記每個(gè)執(zhí)行方法的Event
- 當(dāng)活動(dòng)狀態(tài)改變時(shí)光戈,系統(tǒng)會(huì)判斷即將要改變成的狀態(tài)
- 根據(jù)狀態(tài)獲取要執(zhí)行的Event
- 從注冊(cè)的Observer中獲取標(biāo)注為對(duì)應(yīng)的Event哪痰,執(zhí)行邏輯