使用lifecycle解耦页面与组件

文章目录

    • Lifecycle原理介绍
    • Lifecycle对Activity生命周期监听
    • Lifecycle对Service生命周期监听
    • ProcessLifecycleOwner监听APP的前后台

Lifecycle原理介绍

Jetpack为我们提供了两个类,LifecyleOwner(被观察者)和LifecyleObserver(观察者),通过观察者模式实现对页面生命周期的监听。

Lifecycle对Activity生命周期监听

  • 绑定activity的生命周期
class LifeCycleListener(val activity: Activity, private val onChangeListener: OnChangeListener) :LifecycleObserver {
    companion object{
        init {
            Log.e("tagLifeCycleListener", ": 执行" )
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    private fun onCreated(){
        onChangeListener.onChange("回调 onCreated 执行")
        Log.e("tagLifeCycleListener", ":onCreated 执行" )
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    private fun onResume(){
        Log.e("tagLifeCycleListener", ":onResume 执行" )
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    private fun onPause(){
        Log.e("tagLifeCycleListener", ":onPause 执行" )
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    private fun onDestroy(){
        Log.e("tagLifeCycleListener", ":onDestroy 执行" )
    }

    interface OnChangeListener{
        fun onChange(des:String)
    }
}
  • 将观察者和被观察者绑定
 Log.e(TAG, "onCreate: 执行", )
        lifecycleListener = LifeCycleListener(this, object : LifeCycleListener.OnChangeListener {
            override fun onChange(des: String) {
                Log.e(TAG, "接受到 onChange: ")
            }

        })
        //lifecycle将观察者和被观察者绑定,解决组件对activity生命周期的依赖问题
        lifecycle.addObserver(lifecycleListener)
        
 override fun onResume() {
        super.onResume()
        Log.e(TAG, "onResume: 执行", )
    }

    override fun onPause() {
        super.onPause()
        Log.e(TAG, "onPause: 执行", )
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.e(TAG, "onDestroy: 执行", )
    }

使用lifecycle解耦页面与组件_第1张图片

Lifecycle对Service生命周期监听

  • LifecycleObserver观察者
class MyServiceObserver :LifecycleObserver{
    private  val MyServiceObserverTAG = "MyServiceObserver"

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    private fun onStart(){
        Log.e(MyServiceObserverTAG, "start: 执行" )
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    private fun onDestroy(){
        Log.e(MyServiceObserverTAG, "onDestroy: 执行" )
    }

}
  • LifecycleService的绑定
class MyService: LifecycleService() {
    private  var myServiceObserver:MyServiceObserver = MyServiceObserver()
    init {
        lifecycle.addObserver(myServiceObserver)
    }
}
fun launchService(view: View) {
        startService(Intent(this,MyService::class.java))
    }
    fun closeService(view: View) {
        stopService(Intent(this,MyService::class.java))
    }

使用lifecycle解耦页面与组件_第2张图片

ProcessLifecycleOwner监听APP的前后台

  • App处于前台或者后台的监听
class ApplicationObserver:LifecycleObserver {
    private  val ApplicationObserverTAG = "ApplicationObserver"

    /**
     * 监听应用程序的onCreate方法,整个生命周期只会调用一次
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    private fun onCreated(){
        Log.e(ApplicationObserverTAG, "Application onCreated: 执行" )
    }

    /**
     * 监听应用程序的处于前台时调用
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    private fun onStart(){
        Log.e(ApplicationObserverTAG, "Application onStart: 前台执行" )
    }

    /**
     * 监听应用程序的处于前台时调用
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    private fun onResume(){
        Log.e(ApplicationObserverTAG, "Application onResume: 前台执行" )
    }

    /**
     * 监听应用程序的处于后台时调用
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    private fun onPause(){
        Log.e(ApplicationObserverTAG, "Application onPause: 后台执行" )
    }

    /**
     * 监听应用程序的处于后台时调用
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    private fun onStop(){
        Log.e(ApplicationObserverTAG, "Application onStop: 后台执行" )
    }

    /**
     * 永远不会调用,系统不会分发调用on_destroy事件
     */
    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    private fun onDestroy(){
        Log.e(ApplicationObserverTAG, "Application onDestroy: 不会执行" )
    }
}

class APP: Application() {
    override fun onCreate() {
        super.onCreate()
        ProcessLifecycleOwner.get().lifecycle.addObserver(ApplicationObserver())
    }
}

使用lifecycle解耦页面与组件_第3张图片
lifecycle示例代码

你可能感兴趣的:(Jetpack,Jetpack组件,lifecycle用法,APP前台后台监控)