Skip to content

Android Activity Lifecycle with example in Kotlin

Android Activity Lifecycle: is managing the state of Activity like when its start, stop, user, using, not in front of the user, no more longer. So that all states are managing by call back methods in action. You can override those methods and can do a particular operation to do the best output of your application.

Like your application is going in the background and you want to save some data, in this case, you have to know about Android Activity Lifecycle.

Android Activity Lifecycle with example in Kotlin

Why Use Activity Lifecycle callback methods?

If you do a good implementation of Lifecycle call-backs methods in your android app it will avoid many problems, Like :

  • If the user files the form and suddenly rotates the screen so all filled data will be lost, It’s not good.
  • Android  limited resources can crash your app if you didn’t release it on time
  • Losing the user’s progress if they leave your app and return to it at a later time.

Many more problems you can save by just using the proper guideline of Activity Lifecycle.

Activity has six states

  • Created
  • Started
  • Resumed
  • Paused
  • Stopped
  • Destroyed

Activity lifecycle has seven methods

  • onCreate()
  • onStart()
  • onResume()
  • onPause()
  • onStop()
  • onRestart()
  • onDestroy()

Practical Situations : 

Lets see what happens when activity going through deferents state

  • When open the app
    onCreate() –> onStart() –>  onResume()
  • When back button pressed and exit the app
    onPaused() — > onStop() –> onDestory()
  • When home button pressed
    onPaused() –> onStop()
  • After pressed home button when again open app from recent task list or clicked on app icon
    onRestart() –> onStart() –> onResume()
  • When open app another app from notification bar or open settings
    onPaused() –> onStop()
  • Back button pressed from another app or settings then used can see our app
    onRestart() –> onStart() –> onResume()
  • When any dialog open on screen
    onPause()
  • After dismiss the dialog or back button from dialog
    onResume()
  • Any phone is ringing and user in the app
    onPause() –> onResume()
  • When user pressed phone’s answer button
    onPause()
  • After call end
    onResume()
  • When phone screen off
    onPaused() –> onStop()
  • When screen is turned back on
    onRestart() –> onStart() –> onResume()

Let’s come into deep: Lifecycle callbacks

android activity lifecycle

onCreate()

When the android system first creates the activity, it will call onCreate(). Used to initialize the activity, for example, create the user interface.

onStart()

When the activity enters the Started state, the system invokes this callback. The onStart() call makes the activity visible to the user, as the app prepares for the activity to enter the foreground and become interactive.

onResume()

When the activity enters the Resumed state, it comes to the foreground, and then the system invokes the onResume()callback. This is the state in which the app interacts with the user.

onPause()

The system calls this method as the first indication that the user is leaving your activity (though it does not always mean the activity is being destroyed); it indicates that the activity is no longer in the foreground (though it may still be visible if the user is in multi-window mode).

onStop()

When your activity is no longer visible to the user, it has entered the Stopped state, and the system invokes theonStop() callback. This may occur, for example, when a newly launched activity covers the entire screen.

onRestart()

From the Stopped state, the activity either comes back to interact with the user, or the activity is finished running and goes away. If the activity comes back, the system invokes onRestart()

onDestroy()

Android system invokes onDestroy() method before the activity is destroyed. Activity is destroyed because of :

  1. the activity is finishing (due to the user completely dismissing the activity or due to finish() being called on the activity), or
  2. the system is temporarily destroying the activity due to a configuration change (such as device rotation or multi-window mode)
  3. or a memory issue in the app

Some more callbacks methods added in activity lifecycle for saving and retrieving data :

onSaveInstanceState(Bundle outState)

As your activity begins to stop, the system calls the methodonSaveInstanceState() so your activity can save state information to an instance state bundle.

onRestoreInstanceState(Bundle savedInstanceState)

When your activity is recreated after it was previously destroyed, you can recover your saved instance state from the Bundle that the system passes to your activity. Both the onCreate() and onRestoreInstanceState() callback methods receive the same Bundle that contains the instance state information.

Reference: https://developer.android.com/guide/components/activities/activity-lifecycle

Check the code :

Step 1. Create new project “Build Your First Android App in Kotlin
Step 2. Open MainActivity.kt kotlin class and override callbacks methods

add a print method with a log.

package `in`.eyehunt.androidactivitylifecyclekotlin

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        print("onCreate")
    }

    override fun onStart() {
        super.onStart()
        print("onStart")
    }

    override fun onResume() {
        super.onResume()
        print("onResume")
    }

    override fun onPause() {
        super.onPause()
        print("onPause")
    }

    override fun onStop() {
        super.onStop()
        print("onStop")
    }

    override fun onRestart() {
        super.onRestart()
        print("onRestart")
    }

    override fun onDestroy() {
        super.onDestroy()
        print("onDestroy")
    }

    fun print(msg: String){
        Log.d("Activity State ",msg)
    }
}
Step 3. Run the application, in the emulator or On your Android device

Check the output in Logcat.

Android Activity Lifecycle output

See how to Logcat show

You can test all state. Do every situation as mentioned before in this tutorial.

Download Android activity lifecycle source code in kotlin  :

https://github.com/eyehunt/AndroidActivityLifecycleKotlin

Note: This example (Project) is developed in Android Studio 3.0.1, tested on Android 7.1.1 ( Android Nougat), compile SDK version API 26: Android 8.0 (Oreo)

MinSdkVersion=”15″

TargetSdkVersion=”26″

Coding in Kotlin

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading