Ultimate Guide to Observe Application Lifecycle Events in Android

Recently, Android has provided LifecycleObserver interface to observe Application lifecycle events easily. But I have seen on Stackoverflow that few developers are still struggling in using ‘LifecycleObserver‘ interface. This tutorial will help you to understand how to listen, Application Lifecycle Events. Using ‘LifecycleObserver‘ interface we can easily receive Application Lifecycle Events in Android.

Following is the Java solution if you are looking for Kotlin solution you can visit Kotlin Tutorial.

Steps to Observe Application Lifecycle Events

1. Add the following dependency in your App-level build.gradle file.

implementation 'android.arch.lifecycle:extensions:1.1.1'

2. Create the Application Class if you don’t already have it in your project. For that create a new class with the name ‘ApplicationClass‘.

Add the following code in your Application Class.

import android.app.Application;
import android.util.Log;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.lifecycle.ProcessLifecycleOwner;


public class ApplicationClass extends Application implements LifecycleObserver {

    @Override
    public void onCreate() {
        super.onCreate();

        ProcessLifecycleOwner.get().getLifecycle().addObserver(this); // to observe Application lifecycle events
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onAppStart() {
        Log.e("lifecycle event","ON_START");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void onAppResume() {
        Log.e("lifecycle event","ON_RESUME");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void onAppPause() {
        Log.e("lifecycle event","ON_PAUSE");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onAppStop() {
        Log.e("lifecycle event","ON_STOP");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void onAppDestroy() {
        Log.e("lifecycle event","ON_DESTROY");
    }

}

In the above code, you can remove the extra methods for the events you don’t want to receive.

For those who are using the Support Library instead of Androidx, change the above imports with the following.

import android.app.Application;
import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.OnLifecycleEvent;
import android.arch.lifecycle.ProcessLifecycleOwner;
import android.util.Log;

3. Don’t forget to add Application class reference in your ‘AndroidManifest.xml‘ file, if you have created a new Application class in step 2. For that, Add the following property in your ‘application‘ tag.

android:name=".ApplicationClass"

That’s it. Enjoy 🙂

You can also find other useful helping articles, code snippets and tutorials in our Coding Articles & Tutorials Knowledge Base.

Native Mobile Application Developer (Android + IOS) having experience in Java, Swift, Kotlin, Objective C, Unity, C#, C/C++, NODE JS & PHP.

2 thoughts on “Ultimate Guide to Observe Application Lifecycle Events in Android

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.