Android Activity LifeCycle


In order to create Android Activity, we need to extend Activity class of package & over-ride its built-in Activity LifeCycle methods. To customize behaviour of our Activity we need to understand some basics of Android Activity Lifecycle. Activity LifeCycle is the way our Activity works i.e. how an Activity is created, started, paused, resumed, stopped, destroyed etc., which you can see in below fig.


Android Activity LifeCycle


Explaining Android Activity Lifecycle Methods


Android Activity LifeCycle onCreate

onCreate() is the entry point to any Activity as it is called every time a new Activity is invoked. This method will instantiate variables, arrays, other storage identifiers, prepares bitmaps to be displayed, create views  etc. In other words it will make resources which will be required to start our Activity.




Android Activity LifeCycle onCreate

onStart() is the next method called after onCreate(), when our application is being visible to the user. Differing from onCreate() method, onStart() is also invoked when our application is brought to front from background ( i.e. minimized state to running state ).




Android Activity LifeCycle onCreateonResume() is called whenever our application is on top of Activity stack ( i.e. when user is interacting with our Application). Whenever we start our minimized Activity onResume() method will be called. It will bring all saved instances saved by onPause() into RAM & makes them ready for use.




Android Activity LifeCycle onCreateonPause() is called when our application is minimised or when we send current Activity to background to start new Activity. This method will save all temporary variables, preferences & other instances of data which we will require on resuming. Its key focus should be to free up main memory for upcoming Activity.




Android Activity LifeCycle onCreateonStop() will be called when our Activity is no more visible to user. It may be called in response to switching to new Activity or closing our current Activity by pressing back-key.





Android Activity LifeCycle onCreateonDestroy() method is called to reclaim main memory & other system resources occupied by any of our preferences, variables, bitmaps, Context,etc. by wiping all the data for that Activity. When we press back-key this method is called. Calling finish() method will directly invoke onDestroy().




Sequence of Activity LifeCycle methods which will be called based on various situations




  • Re-launching a minimized Application or re-starting Activity which was sleeping in background due to another running Activity :
    1. onRestart()
    2. onStart()
    3. onResume()

    ( Difference between onResume() & onRestart() is that onRestart() doesn’t gets triggered when we start a new Activity.)



  • Using finish() method inside your current Activity.
    1. onDestroy()



Source Code : Activity


Now Let us work on Project : (Refer – Configuring Existing AndroidStudioProject) Activity LifeCycle which you might have downloaded from above or you can follow the scripts below by copying it to implement  in your Project. To launch Downloaded project, Extract the zip, copy it into AndroidStudioProjects. Now from Android Studio click open Existing Android Studio Project and select the path to extracted file we just copied in AndroidStudioProjects directory. If Sync SDK path prompt comes just click OK and you are ready to work. If still you get error check for AndroidStudioProjects/ActivityLifeCycle/app/build.gradle , it will contain different SDK Version. Change it to SDK Version which you have.




package com.nestedif.activitylifecycle;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class MainActivity extends Activity {
   String TAG = "Android : ";
   /* Called when our activity is created for the First time. */
   public void onCreate(Bundle savedInstanceState) {
      Toast.makeText(getApplicationContext(), " onCreate() method",Toast.LENGTH_LONG).show();
      Log.d(TAG, " onCreate() method");
      //	Try Removing below comment & see what happens on switching over to new Activity without closing current Activity
      Intent in = new Intent(this,MainActivity2.class);
      //  	Try Removing below comment & see what happens on switching over to new Activity on closing current Activity
      Intent in = new Intent(this,MainActivity2.class);

   /* Called everytime new Activity is created or we resume our current activity. */
   protected void onStart() {
      Toast.makeText(getApplicationContext(), " onStart() method",Toast.LENGTH_LONG).show();
      Log.d(TAG, " onStart() method");

   /* Called when come back to current activity from minimized state or closing any other running activity or launching activity for 1st time. */
   protected void onResume() {
      Toast.makeText(getApplicationContext(), " onResume() method",Toast.LENGTH_LONG).show();
      Log.d(TAG, " onResume() method");

   /* Called when we launch our Activity, which was minimized previously (launching Activity which was in background)  */
   protected void onRestart() {
      Toast.makeText(getApplicationContext(), " onRestart() method",Toast.LENGTH_LONG).show();
      Log.d(TAG, " onRetart() method");
   /* Called when we switch over to another activity with or without closing current activity or minimizing current activity */
   protected void onPause() {
      Log.d(TAG, " onPause() method");
      Toast.makeText(getApplicationContext(), " onPause() method",Toast.LENGTH_LONG).show();

   /* Called when either we start Another activity or close our Current activity by pressing Back button. */
   protected void onStop() {
      Log.d(TAG, " onStop() method");
      Toast.makeText(getApplicationContext(), " onStop() method",Toast.LENGTH_LONG).show();

   /* Called when our activity is going to destroy and all its variables will be wiped by the garbage collector. */
   public void onDestroy() {
      Log.d(TAG, " onDestroy() method");
      Toast.makeText(getApplicationContext(), " onDestroy() method",Toast.LENGTH_LONG).show();

Explaining above Code :

  • We have added all the Activity LifeCycle methods – onCreate(), onStart(), onResume(), onRestart(), onPause(), onStop(), onDestroy() of our Activity Class, which will be invoked by the system based on various situations as explained above. Inside them we have placed Toast  &  Log so that we can monitor at what time the method is invoked.
  • Line 25-28  &  Line 32-36 Code to Start new Activity using intent which you can test by removing comments.



2. activity_main.xml : Layout file

<RelativeLayout xmlns:android=""
    tools:context=".MainActivity" >

        android:text="To Check Life Cycle of MainActivity " />

  • Inside our activity_main.xml we have simply added a static TextView.



Try it yourself :

  1. Whenever we change orientation of our device i.e. switch between portrait mode(vertical) & Landscape mode (horizontal), our Activity is Destroyed & Re-created. So you will see onPause() > onStop() > onDestroy() > onCreate() > onStart() > onResume() happening in this sequence. So we need to keep in mind to save our temp. data in such situations. You can check this by pressing Ctrl + F11  (MAC – CMD+F11) from your AVD.
  2. Remove The Comment blocks at Line-25-28  & Line-32-36 one-by-one. Run again the application & see how do Activity LifeCycle happens with launching new Activity.



Report Errors + Bugs & Become Insider for

We would like to hear you, if you find any error or misspelled phrase while reading our tutorials. By reporting mistakes through email to you could help other peers.