Android Jetpack架構之Lifecycle
阿新 • • 發佈:2021-08-28
一、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耦合降低,程式碼出更簡潔。