Android Activity LifeCycle

 

In order to create Android Activity, we need to extend Activity class of package android.app & 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()

 

 



Download
Source Code : Activity Lifecycle.zip


 

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.

 

 

1. MainActivity.java

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

  • 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 Nestedif.com

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