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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM