背景
Android
組件(Activity、Fragment)
是有生命週期的,在不同的生命週期方法做不同的操作。比如在onStar
做一些初始化的操作,在onStop
做些輕量的銷燬操作。在使用 MVP
架構開發的時候,我們一般會定義一個BasePreserter
來處理業務生命週期方法調用時處理相關操作,但是業務複雜後還是避免不了在組件的生命週期方法中去做處理。
常見的是地圖的定位和銷燬。相信用過百度獲取高德地圖定位SDK
的胖友,對下面的代碼絕對熟悉:
// 自定義的定位監聽類
internal class MyLocationListener(
private val context: Context,
private val callback: (Location) -> Unit
) {
fun start() {
// 定位信息初始化
}
fun stop() {
// 定位信息銷燬
}
}
class MyActivity : AppCompatActivity() {
private lateinit var myLocationListener: MyLocationListener
override fun onCreate(...) {
myLocationListener = MyLocationListener(this) { location ->
// 更新UI
}
}
public override fun onStart() {
super.onStart()
// 傳遞生命週期
myLocationListener.start()
// 其他對象需要生命週期也會在這傳遞
}
public override fun onStop() {
super.onStop()
// 傳遞生命週期
myLocationListener.stop()
// 其他對象需要生命週期也會在這傳遞
}
}
當需要組件生命週期的類過多,那麼組件生命週期方法中就會出現一堆的生命週期傳遞方法,不僅維護不方便。而且組件生命週期方法都是在主線程中運行,過多的在生命週期方法中執行代碼,有可能會引發內存泄露甚至 應用崩潰。
因此Google
在Jetpack
庫中提供了Lifecycles
類,用於存儲有關組件(如Activity
或 Fragment
)的生命週期狀態的信息,並允許其他對象觀察此狀態。
Lifecycles 是什麼
生命週期感知組件,主要是用於處理生命週期的相關的操作。
在使用MVP
架構開發時,爲了在組件的不同生命週期中做相關操作,我們不得不在組件的生命週期調用Present
中獲取數據的方法,然後在調用View
的回調接口更新UI。使用Lifecycles
可以輕鬆集成這些組件,而無需在組件進行手動生命週期管理。
Lifecycles 使用步驟
Module -> build.gradle
的引入
版本依賴查看:https://developer.android.google.cn/jetpack/androidx/releases/lifecycle#declaring_dependencies
def lifecycle_version = "2.2.0"
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
// Lifecycles only (without ViewModel or LiveData)
implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version"
// 還有一些可選項,可查看上面的版本依賴鏈接
- 創建
presenter
繼承LifecycleObserver
interface IPresenter : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onStart(owner: LifecycleOwner)
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy(owner: LifecycleOwner)
}
- 創建
Activity
(推薦創建Fragment+ViewModel
)實現LifecyclesOwner
,通過Lifecycle.Event
指定生命週期,如下的MineContract.Presenter
interface MineContract {
interface View:IBaseView{
// 顯示刷新後的 UI
fun refreshUI()
}
interface Presenter:IPresenter<View>, LifecycleObserver {
// 獲取數據
fun getData()
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onStart(owner: LifecycleOwner)
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun onResume(owner: LifecycleOwner)
}
}
- 在組件頁面創建
Presenter
實例,訂閱該實例即可。
private val minePresenter = MinePresenter(this)
override fun initView() {
// 訂閱事件
lifecycle.addObserver(minePresenter)
btn_get_data.setOnClickListener {
minePresenter.getData()
}
}
詳細使用代碼請參見:YGragon/FrameDemo
如果我們使用了ViewModel + LiveData
的模式,那麼Lifecycles
就可不用單獨引用了,因爲LiveData
也是生命週期感知組件。
總結
使用Lifecycles
庫可以感知組件(Activity/Fragment)
的生命週期,從而在相應的生命做正確的事。實現App
的穩定運行,同時增強App
的可維護性。
在Jetpack
庫中,還有LiveData
庫也可以感知組件的生命週期感。下篇文章介紹ViewModel+LiveData
使用。
參考
上車
佛系原創號主