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

posted @ 2021-08-28 15:05  naray  阅读(293)  评论(0编辑  收藏  举报