Easiest Way to Observe Application Lifecycle Events in Kotlin Android

Are you looking to observe events like OnStart, OnResume, OnPause, OnStop & OnDestroy for your Android Application in Kotlin? This guide shows the easiest way to listen & receive lifecycle events related to your Application. In this tutorial ‘LifecycleObserver‘ interface is used. Using this interface we can easily observe Application Lifecycle Events in Kotlin Android.

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

Steps to Observe Application Lifecycle Events in Kotlin

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

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

2. Now you need to create the Application class if you don’t already have it in your project. For that purpose 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

class ApplicationClass : Application(), LifecycleObserver {

    override fun onCreate() {
        super.onCreate()

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

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

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

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

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

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

You can remove the extra methods from the above lifecycle events, which you don’t want to receive.

If you 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. If you have created a new Application class in step 2. You also need to add Application class reference in your ‘AndroidManifest.xml‘ file. Add the following name property in ‘application‘ tag.

android:name=".ApplicationClass"

That’s it. This is how to observe Application Lifecycle Events in Kotlin.

Enjoy 🙂

Feel free to ask your questions in the comments section, if you face any difficulty.

Also, visit our Coding Articles & Tutorials Knowledge Base for other helping code snippets.

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

3 thoughts on “Easiest Way to Observe Application Lifecycle Events in Kotlin Android

  1. Hmm is anyone else experiencing problems with the pictures on this blog loading? I’m trying to determine if its a problem on my end or if it’s the blog. Any suggestions would be greatly appreciated.

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.