• Android Jetpack架构之Lifecycle


    一、Lifecycle是什么?

      Lifecycle生命周期感知组件,可执行操作响应另一个组件(Activity或者Fragment)的生命周期状态。

    二、Lifecycle出现的背景

      用于解耦系统组件与其它组件的生命周期。

    三、示例

      App中都有开屏广告,在开屏广告右上角一个倒计时功能。倒计时功能需要和App进入后台暂停,进入前台继续的。

      在没有Lifecycle之前,代码实现:

      RCountDownTimeView实现

    class RCountDownTimeView constructor(context: Context, attrs: AttributeSet?) :
        androidx.appcompat.widget.AppCompatTextView(context, attrs) {
    
        private var _handler: Handler? = null
        private var _countDownRunnable: Runnable? = null
        private var _seconds: Int = 5
    
        init {
            _handler = Handler(Looper.getMainLooper())
        }
    
        private fun startCountDown() {
            if (_seconds <= 0) {
                stopCountDown()
                return
            }
    
            _countDownRunnable = Runnable {
                this.text = "${--_seconds}"
                startCountDown()
            }
            _countDownRunnable?.let {
                _handler?.postDelayed(it, 1000)
            }
        }
    
        private fun stopCountDown() {
            _countDownRunnable?.let {
                _handler?.removeCallbacks(it)
            }
            _countDownRunnable = null
        }
    
        fun setStartTime(seconds: Int) {
            _seconds = seconds
        }
    
        fun start() {
            startCountDown()
        }
    
        fun pause() {
            stopCountDown()
        }
    
        fun cancel() {
            stopCountDown()
            _handler?.removeCallbacksAndMessages(null)
            _handler = null
    } }

      Activity实现:

    class MainActivity : AppCompatActivity() {
    
        private val _countDownTime: RCountDownTimeView by lazy {
            countDownTime
        }
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            _countDownTime.setStartTime(10)
        }
    
        override fun onResume() {
            super.onResume()
    
            _countDownTime.start()
        }
    
        override fun onPause() {
            super.onPause()
    
            _countDownTime.pause()
        }
    
        override fun onDestroy() {
            super.onDestroy()
    
            _countDownTime.cancel()
        }
    
    }

      从上面例子可以看出,倒计时组件与Activity组件生命周期耦合严重。

      使用Lifecycle后,实现:

      RCountDownTimeView实现

    class RCountDownTimeView constructor(context: Context, attrs: AttributeSet?) :
        androidx.appcompat.widget.AppCompatTextView(context, attrs), LifecycleObserver {
    
        private var _handler: Handler? = null
        private var _countDownRunnable: Runnable? = null
        private var _seconds: Int = 5
    
        init {
            _handler = Handler(Looper.getMainLooper())
        }
    
        private fun startCountDown() {
            if (_seconds <= 0) {
                stopCountDown()
                return
            }
    
            _countDownRunnable = Runnable {
                this.text = "${--_seconds}"
                startCountDown()
            }
            _countDownRunnable?.let {
                _handler?.postDelayed(it, 1000)
            }
        }
    
        private fun stopCountDown() {
            _countDownRunnable?.let {
                _handler?.removeCallbacks(it)
            }
            _countDownRunnable = null
        }
    
        fun setStartTime(seconds: Int) {
            _seconds = seconds
        }
    
        @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
        fun start() {
            startCountDown()
        }
    
        @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
        fun pause() {
            stopCountDown()
        }
    
        @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
        fun cancel() {
            stopCountDown()
            _handler?.removeCallbacksAndMessages(null)
        }
    
    }

      Activity实现:

    class MainActivity : AppCompatActivity() {
    
        private val _countDownTime: RCountDownTimeView by lazy {
            countDownTime
        }
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            lifecycle.addObserver(_countDownTime)
            _countDownTime.setStartTime(10)
    
        }
    
        override fun onDestroy() {
            super.onDestroy()
    
            lifecycle.removeObserver(_countDownTime)
        }
    
    }

      在使用LifeCycle后,与系统组件Activity耦合降低,代码出更简洁。

    四、Lifecycle工作原理

    五、LifecycleService

    六、ProcessLifecycleOwner

  • 相关阅读:
    力扣第945题 使数组唯一的最小增量
    力扣第365题 水壶问题
    力扣面试题40 最小的k个数
    力扣第409题 最长回文串
    力扣第46题 全排列
    力扣第1160题 拼写单词
    力扣面试题01.06 字符串压缩
    力扣第695题 岛屿的最大面积
    树莓派 鼠标自动消失
    树莓派 VNC 远程桌面 同一个桌面
  • 原文地址:https://www.cnblogs.com/naray/p/15196249.html
Copyright © 2020-2023  润新知