Callbacks for When a User Exits Your Android App

Were you ever working on a feature for your app where you wanted to detect when the user quits your app - either by pressing the back button or the home button? Well then, this article is for you.

Callbacks for When a User Exits Your Android App

Were you ever working on a feature for your app where you wanted to detect when the user quits your app - either by pressing the back button or the home button? Well then, this article is for you.

There is a very simple solution for detecting when the user quits your app, and it enters background (or foreground when he reopens the app) - Process Lifecycle Owner.

Process Lifecycle Owner provides the lifecycle for the entire application instead of individual activities.

You can consider this LifecycleOwner as the composite of all of your Activities, except that ON_CREATE will be dispatched once and ON_DESTROY will never be dispatched. Other lifecycle events will be dispatched with the following rules: ProcessLifecycleOwner will dispatch ON_START, ON_RESUME events, as a first activity moves through these events. ON_PAUSE, ON_STOP, events will be dispatched with a delay after the last activity passed through them. This delay is long enough to guarantee that ProcessLifecycleOwner won't send any events if activities are destroyed and recreated due to a configuration change.

Implementation

In your app/build.gradle file add the following dependencies.

implementation "androidx.lifecycle:lifecycle-process:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-common:$lifecycle_version"

In case you haven't defined lifecycle_version make sure that you define it with def lifecycle_version = "2.4.1". At the time of writing this article, the latest stable version was 2.4.1. You can check for the latest version on Lifecycle Docs.

Next, in the onCreate() of your application class, add an observer for the Process Lifecycle Owner.

// Kotlin
class App: Application() {
	override fun onCreate() {
    	super.onCreate()
        ProcessLifecycleOwner.get().getLifecycle().addObserver( AppLifecycleListener())
    }
}

Now, create this listener in a new file, or in the same Application class (not recommended). Android studio should take care of the imports for you.

class AppLifecycleListener : DefaultLifecycleObserver {

	val TAG = "AppLifecycleListener"
    
    override fun onStart(owner: LifecycleOwner) {
    	Log.i(TAG, "Moved to foreground")
    }

    override fun onStop(owner: LifecycleOwner) {
    	Log.i(TAG, "Moved to background")
    }
}

Here, the function onStop() would be called each time the user moves out of the app. When you press the back button multiple times and come out of the app or when you press the home button and come out, when you press the recents button, when you lock your phone and so on.

The onStart() function would be called each time the user opens the app. You can add the required code that you want to be executed when the user quits the app inside the onStop() function.

PS: You can also override the other lifecycle functions like onPause() or onResume() as per your needs.

Hope this helps. If you have any questions feel free to reach out to me on Twitter.