Android Activity Lifecycle – 7 Stages and Functions

Filed Under: Android

Activity is one of the primary components of an Android application. In this tutorial, we’ll be looking at the android activity lifecycle, and the various functions invoked in the different stages of an Activity’s runtime.

Android Activity Lifecycle Flow Diagram

An Activity is a single focused thing that the user can see. Let’s see how the lifecycle of an Activity looks like, which functions are triggered when and why, through a flow diagram.


Android Activity Lifecycle Stages

The android activity goes through following lifecycle stages.

  • Create
  • Start
  • Pause
  • Resume
  • Stop
  • Restart
  • Destroy

There are specific callback functions for each of these lifecycle stages.

Android Activity Lifecycle Functions

Let’s look at the lifestyle functions of an activity.

1. onCreate()

This is the first stage of an activity lifecycle. This function is called when the activity is initialized.

In our Kotlin Activity class, the following function defines onCreate():

fun onCreate(savedInstanceState: Bundle?)

We’ll look at what the bundle value contains in a later section.

2. onStart()

In this lifecycle stage, the activity is ready to process. This function is called when the activity comes into the foreground but it isn’t interactive yet.

3. onResume()

This is where the activity is executing. This function is called when the user is interacting with the activity.

4. onPause()

This function is called when the user interaction with the activity isn’t possible.

Common scenarios: An incoming call comes up. Another application pops up. With the introduction of Android N and the multi-window feature, multiple apps are visible on the screen at one time. Apps that are not in focus would have the onPause() method triggered.

5. onStop()

This function gets triggered when the activity is no longer visible and it has stopped working.

6. onDestroy()

This function gets triggered when the activity is destroyed.

Common scenarios: A new activity starts and the current activity is killed. For example, pressing back button to exit the application.

7. onRestart()

This function gets triggered whenever the activity starts after being stopped. It differs from onStart() in the fact that it won’t be called when the activity launches for the first time. In the later stages, it’ll always be called before onStart().

Saving and Restoring Activity States

Android provides us with a pair of functions : onSaveInstanceState() and onRestoreInstanceState().

Following are the major scenarios when your activity’s state is saved.

  1. When onStop() is triggered.
  2. On configuration changes such as screen rotations, changes in screen sizes. Android N has multi-window features, so user often changes the window size of the applications.

Configuration changes generally trigger the application to recreate the current activity by destroying the current view to draw it again. We need to save the current state of the activity in the onSaveInstanceState() function as shown below.

override fun onSaveInstanceState(outState: Bundle?) {

The Bundle object stores the field values from the activity in the form of key/value pairs.

The Bundle object is of the type nullable to prevent NullPointerException, thanks to Kotlin.

override fun onRestoreInstanceState(savedInstanceState: Bundle?) {

The onRestoreInstanceState() function is only called when a saved state exists.

Note: Android automatically takes care of saving and restoring Views that have a unique ID.

Hence in the below layout, only the value of TextView would be saved and restored.

<?xml version="1.0" encoding="utf-8"?>

        android:text="Hello World!"
        app:layout_constraintTop_toTopOf="parent" />

        android:text="CLICK ME"
        app:layout_constraintTop_toBottomOf="@+id/textView" />


To disable saving the view’s state, set the attribute android:saveEnabled to false

For Custom Views we need to explicitly set setSavedEnabled(true) on the widget.

Following code snippet demonstrates saving and restoring state in an activity.

import android.os.Bundle

class MainActivity : AppCompatActivity() {

    var title = "This title variable would reset when the configuration is changed. Let's preserve it"

    override fun onCreate(savedInstanceState: Bundle?) {


    override fun onSaveInstanceState(outState: Bundle?) {

    override fun onRestoreInstanceState(savedInstanceState: Bundle?) {

The onRestoreInstanceState() function is called only when recreating activity after it was killed by the Android OS.

Note: Alternatively, the saved instance values can be retrieved in the onCreate function too. But we’ll have to keep a null checker since the bundle object would be null when the activity is created for the first time.

override fun onCreate(savedInstanceState: Bundle?) {

        if (savedInstanceState != null) {
        } else

Handling Activity Configuration Changes Ourselves

We can set the android:configChanges attribute in the AndroidManifest.xml file to prevent recreating the activity on a configuration change.

<activity android:name=".MainActivity"

The following method gets triggered whenever any of the above configuration change happens.

override fun onConfigurationChanged(newConfig: Configuration?) {

References: Google Docs

Leave a Reply

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

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages