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使用。

参考

上车

佛系原创号主
在这里插入图片描述

原文地址:https://www.cnblogs.com/gdragon/p/13210562.html