Android ProcessLifecycleOwner observes the process life cycle

Introduction

ProcessLifecycleOwnerLiteral translation, that is, the owner of the process life cycle.
Learned through the DOC comments:

  1. The Lifecycle.Event.ON_CREATE event will be received once after the process starts;
  2. Lifecycle.Event.ON_DESTROY event, never received;
  3. After the onStart and onResume of the first activity, the owner will receive the same type of events Lifecycle.Event.ON_START, Lifecycle.Event.ON_RESUME;
  4. After the last Activity's onPause and onStop, the owner will receive the same type of events Lifecycle.Event.ON_PAUSE, Lifecycle.Event.ON_STOP (the system guarantees that the received events are normal, and will not 配置更改而销毁和重新创建活动send such events in time)

It is very convenient to judge the front and back of the process

It itself is a singleton implementation, source code:

@NonNull
public static LifecycleOwner get() {
    
    
    return sInstance;
}

use

rely

api "androidx.lifecycle:lifecycle-process:2.5.1"

Usage 1, combined with LiveData

Observation of data changes will depend on the activity event of the process

liveData.observe(ProcessLifecycleOwner.get(), Observer {
    
    
    when (ProcessLifecycleOwner.get().lifecycle.currentState) {
    
    
    	Log.i("ProcessLifecycleOwner", "state: ${
      
      ProcessLifecycleOwner.get().lifecycle.currentState}")
        Lifecycle.State.STARTED, Lifecycle.State.RESUMED -> {
    
    
           ...
        }
        else -> {
    
    }
    }
})

It should be noted that the constants in Lifecycle.State and Lifecycle.Event are not in one-to-one correspondence.
Through testing, it is found that when the application returns to the foreground from the background, the log will print "state: STARTED"; when the application is in the foreground, change the The value of liveData, the log will print "state: RESUMED"

Usage 2, get the lifecycle instance of the owner, and add observers to the lifecycle

ProcessLifecycleOwner.get().lifecycle.addObserver(LifecycleObserver observer) 

Looking LifecycleObserverat the DOC comments for , it is recommended not to use this class directly; instead use its subclasses DefaultLifecycleObserverorLifecycleEventObserver

Example (can be used to judge the front and back of the process):

ProcessLifecycleOwner.get().lifecycle.addObserver(object: DefaultLifecycleObserver {
    
    
    override fun onCreate(owner: LifecycleOwner) {
    
    
        super.onCreate(owner)
    }

    override fun onStart(owner: LifecycleOwner) {
    
    
        super.onStart(owner)
    }

    override fun onResume(owner: LifecycleOwner) {
    
     // 应用前台
        super.onResume(owner)
    }

    override fun onPause(owner: LifecycleOwner) {
    
     // 应用后台
        super.onPause(owner)
    }

    override fun onStop(owner: LifecycleOwner) {
    
    
        super.onStop(owner)
    }

    override fun onDestroy(owner: LifecycleOwner) {
    
    
        super.onDestroy(owner)
    }
})

ProcessLifecycleOwner.get().lifecycle.addObserver(object: LifecycleEventObserver {
    
    
    override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
    
    
        when (event) {
    
    
        	Lifecycle.Event.ON_CREATE -> {
    
    
        		Log.e("ProcessLifecycleOwner", "ON_CREATE")
        	}
            Lifecycle.Event.ON_START -> {
    
    
                Log.e("ProcessLifecycleOwner", "ON_START")
            }
            Lifecycle.Event.ON_RESUME -> {
    
     // 应用前台
                Log.e("ProcessLifecycleOwner", "ON_RESUME")
            }
            Lifecycle.Event.ON_PAUSE -> {
    
     // 应用后台
                Log.e("ProcessLifecycleOwner", "ON_PAUSE")
            }
            Lifecycle.Event.ON_STOP -> {
    
    
                Log.e("ProcessLifecycleOwner", "ON_STOP")
            }
            else -> {
    
    }
        }
    }
})

Guess you like

Origin blog.csdn.net/jjwwmlp456/article/details/129047730