Lifecycle Principle

Jetpack provides two classes, LifecyleOwner and LifecyleObserver, that listen to the page lifecycle through the observer pattern.

Lifecycle listens for the Activity Lifecycle

  • Bind the activity’s life cycle
class LifeCycleListener(val activity: Activity, private val onChangeListener: OnChangeListener) :LifecycleObserver { companion object{ init { Log.e("tagLifeCycleListener", ": Execute ")}} @ OnLifecycleEvent (Lifecycle. Event. ON_CREATE) private fun onCreated () {onChangeListener. OnChange (" callback onCreated Execute ") the e (" tagLifeCycleListener ", } @onlifecycleEvent (Lifecycle.event.on_resume) private fun onResume(){log.e ("tagLifeCycleListener", } @onlifecycleEvent (Lifecycle.event.on_pause) private fun onPause(){log.e ("tagLifeCycleListener", } @onlifecycleEvent (Lifecycle.event.on_destroy) private fun onDestroy(){log.e ("tagLifeCycleListener", ":onDestroy execute ")} interface OnChangeListener{fun onChange(des:String)}}Copy the code
  • Binds the observer to the observed
Log.e(TAG, "onCreate: execute ",) lifecycleListener = lifecycleListener (this, object: execute) LifeCycleListener. OnChangeListener {override fun onChange (des: String) {the e (TAG, "receive the onChange: ")}}) //lifecycle will bind the observer to the observed, Lifecycle. AddObserver (lifecycleListener) Override Fun onResume() {super.onResume() log.e (TAG, Override fun onPause() {super.onpause () log.e (TAG, "onPause: execute ",)} override fun onPause() {super.onpause () Log. Override fun onDestroy() {super.ondestroy () log.e (TAG, "onDestroy: execute ",)} override fun onDestroy() {super.ondestroy () log.e (TAG, "onDestroy: execute ",)}Copy the code

Lifecycle listens for the Service Lifecycle

  • LifecycleObserver observer
class MyServiceObserver :LifecycleObserver{ private val MyServiceObserverTAG = "MyServiceObserver" @OnLifecycleEvent(Lifecycle.Event.ON_START) private fun onStart(){ Log.e(MyServiceObserverTAG, "start: Execute ")} @onlifecycleEvent (Lifecycle.event.on_destroy) private fun onDestroy(){log.e (MyServiceObserverTAG, "onDestroy: Execute ")}}Copy the code
  • LifecycleService binding
class MyService: LifecycleService() {
    private  var myServiceObserver:MyServiceObserver = MyServiceObserver()
    init {
        lifecycle.addObserver(myServiceObserver)
    }
}
Copy the code
fun launchService(view: View) {
        startService(Intent(this,MyService::class.java))
    }
    fun closeService(view: View) {
        stopService(Intent(this,MyService::class.java))
    }
Copy the code

ProcessLifecycleOwner listens on the front and back of the APP

  • App is in the foreground or background listening
class ApplicationObserver:LifecycleObserver { private val ApplicationObserverTAG = "ApplicationObserver" /** * Listen for the application's onCreate method, */ @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: } /** * will never be called, On_destroy */ @onlifecycleEvent (Lifecycle.event.on_destroy) private fun onDestroy(){ Log.e(ApplicationObserverTAG, "Application onDestroy: not executed ")}}Copy the code
class APP: Application() {
    override fun onCreate() {
        super.onCreate()
        ProcessLifecycleOwner.get().lifecycle.addObserver(ApplicationObserver())
    }
}
Copy the code

Lifecycle sample code