Android Jetpack 庫架構組件 Lifecycles 基礎使用

背景

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()
        // 其他對象需要生命週期也會在這傳遞
    }
}

當需要組件生命週期的類過多,那麼組件生命週期方法中就會出現一堆的生命週期傳遞方法,不僅維護不方便。而且組件生命週期方法都是在主線程中運行,過多的在生命週期方法中執行代碼,有可能會引發內存泄露甚至 應用崩潰

因此GoogleJetpack 庫中提供了Lifecycles 類,用於存儲有關組件(如ActivityFragment)的生命週期狀態的信息,並允許其他對象觀察此狀態。

Lifecycles 是什麼

生命週期感知組件,主要是用於處理生命週期的相關的操作。

在使用MVP架構開發時,爲了在組件的不同生命週期中做相關操作,我們不得不在組件的生命週期調用Present中獲取數據的方法,然後在調用View的回調接口更新UI。使用Lifecycles可以輕鬆集成這些組件,而無需在組件進行手動生命週期管理。

Lifecycles 使用步驟

  1. 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"
    
	// 還有一些可選項,可查看上面的版本依賴鏈接
  1. 創建presenter繼承LifecycleObserver
interface IPresenter : LifecycleObserver {

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun onStart(owner: LifecycleOwner)

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    fun onDestroy(owner: LifecycleOwner)
}

  1. 創建 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)
    }
}
  1. 在組件頁面創建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使用。

參考

上車

佛系原創號主
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章