Lifecycle of Android Jetpack components

1, Introduction to Lifecycles

Lifecycles is a component of Jetpack to solve the problem of life cycle management by monitoring the life cycle. If our class is not an Activity (broadcast \ fragment says otherwise), it has no life cycle. However, in the development process, in MVP mode, we need to call back the View interface in the P layer to update the UI. If the P layer is not handled in the life cycle, memory leakage is easy to occur. In order to make the P layer aware of the life cycle of Activity, the following interfaces are usually defined:

interface IPresenter {
    fun onCreate()
    fun onStart()
    fun onResume()
    fun onPause()
    fun onStop()
    fun onDestroy()

Each Presenter implements IPresenter, and then calls back these interfaces in the corresponding Activity to complete life cycle management.
Lifecycles is introduced to realize this function. It is a class that can be observed by other objects. Lifecycles is implemented based on the observer mode to respond to the changes of the life cycle. There are two enumeration variables, Event and Evenet.

2, Import dependency

def lifecycle_version = "2.0.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-common-java8:2.2.0"
implementation "androidx.lifecycle:lifecycle-reactivestreams-ktx:2.2.0" 
testImplementation "androidx.arch.core:core-testing:2.1.0"

3, Use

We first create an Observer to implement the LifecycleObserver interface, and then listen to the corresponding life cycle through annotation:

class LifecycleListener : LifecycleObserver {
    fun lifecycleOnCreate() {

    fun lifecycleOnStart() {

    fun lifecycleOnResume() {

    fun lifecycleOnPause() {

    fun lifecycleOnStop() {

    fun lifecycleOnDestroy() {

    fun lifecycleOnAny() {

Then add listening in onCreate() or onStart() method of Activity: addObserver() can complete listening (before onResume)

Run the code. Let's simulate three scenarios:
1. After the App is started and runs normally, the App will execute the (lifecycle)onCreate, (lifecycle)onstart, (lifecycle)onresume methods; Then we click the App switch button at the bottom right, but do not switch apps. This will execute the (lifecycle)onPause and (lifecycle)onStop methods;

2. Then we let the App return to the foreground, and then execute the ·, (lifecycle)onStart, (lifecycle)onResume methods;
3. Finally, click the home button to return the App to the background. At this time, the following methods will be executed: (lifecycle)onPause, (lifecycle)onStop. Then we let the App run back to the foreground, and it will execute: onRestart, (lifecycle)onStart, (lifecycle)onResume.

We will find two other phenomena: lifecycle does not execute lifecycle on restart because it does not have this method. After each method of lifecycle restart is called, it will be executed.

Lifecycles' listening is called after onCreate(), onStart(), and onResume(), while onPause(), onStop(), and onDestroy() are called before. The reason is easy to understand: we just need to perform some operations in that time period. What is the use if we call them all the time later?

Tags: Android Lifecycle

Posted by kujtim on Thu, 28 Apr 2022 22:12:31 +0300