Activity and Its Lifecycle in Android

Introduction

 
An Activity is an application's component that enables the user to interact to do a view of something, such as dial the phone, take a photo, send an email, or view a map. Basically the Activity is something that a user is doing or interacting on the screen to view something.
 
For example take an Email application in which there are various components like reading a mail, composing a mail and/or delete a mail. Let us take the example of composing a mail-in in which a user starts texting on the screen. So that is an Activity in which the user is interacting with the screen.
 
An application is a combination of various activities that are loosely bound to each other. Although there are various Activities there is the main Activity that appears on the home page while launching and it is visible at the very first glance when the launching is called the main Activity.
 
The Activity follows the mechanism of a stack-based rule and forms an Activity stack. When a new Activity is launched it is placed on the top of the stack.
 

Activity Lifecycle

 
The Activity lifecycle is a lifespan of an Activity starting from onCreate() until onDestroy() being called. This procedure covers not only one cycle but it covers the following three cycles:
  • Entire Lifetime
  • Visible Lifetime
  • Foreground Lifetime

Entire Lifetime

 
As mentioned above an Activity is started from the creation and the calling of the onCreate() method until the onDestroy() method is called. At least an Activity should do a setup of a "global" state (such as defining the layout) in the onCreate() method and might release all the remaining resources in the onDestroy() method call.
 
Let us take an example in which we create a Thread and it is accessing a resource. The thread starts executing after onCreate() is invoked and continues in the running state until onDestroy() is called.
 

Visible Lifetime

 
The Visible Lifetime of an Activity is the life between the call to onStart() and until onStop() is invoked. During this time, the user can see the Activity on-screen and interacts with it. After onStop() is invoked then it goes to either onDestroy() or the system might call onStart() and onStop() multiple times during the entire lifetime of the Activity, as the Activity alternates between being visible and hidden from the user because after onStop() the Activity is no longer visible.
 

Foreground Lifetime

 
The Foreground Lifetime of an Activity is the life span between the call to onResume() and the call to onPause(). This time, the Activity is in front of all other activities on the screen and tend to take input from the user. An Activity can frequently transition to and from the foreground. Let us take an example such as when onPause() is called when the device goes to sleep or when a dialog appears or the user receives a call from another person. Because this state transition can occur often, the code in these two methods should be fairly lightweight because the heavy code could lead to slow resumption.
 
activity life cycle
Figure: This illustrates the entire life cycle of an Activity and sequential calling of methods.
 
A simple code is given below. It just shows how to write the methods. Here we are not linking any references from other resources, in other words, no other methods are used to take reference and the arguments as well are not given in some methods.
  1. import android.support.v7.app.ActionBarActivity;  
  2. import android.os.Bundle;  
  3. import android.view.Menu;  
  4. import android.view.MenuItem;  
  5.   
  6.   
  7. public class MainActivity extends ActionBarActivity {  
  8.   
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.activity_main);  
  13.     }  
  14.   
  15.     protected void onStart(Bundle savedInstanceState)  
  16.     {  
  17.     super.onStart();  
  18.       setContentView(R.layout.activity_main);  
  19.     }  
  20.     @Override  
  21.     protected void onResume() {  
  22.         super.onResume();  
  23.         // The activity has become visible .  
  24.     }  
  25.     @Override  
  26.     protected void onPause() {  
  27.         super.onPause();  
  28.         }  
  29.         // Another activity is taking focus  
  30.       
  31.     @Override  
  32.     protected void onStop() {  
  33.         super.onStop();  
  34.     }  
  35.         // The activity is no longer visible   
  36.       
  37.     @Override  
  38.     protected void onDestroy() {  
  39.         super.onDestroy();  
  40.         // The activity is about to be destroyed.  
  41.     }  
  42.     public boolean onCreateOptionsMenu(Menu menu) {  
  43.         // Inflate the menu; this adds items to the action bar if it is present.  
  44.         getMenuInflater().inflate(R.menu.main, menu);  
  45.         return true;  
  46.     }  
  47.   
  48.     @Override  
  49.     public boolean onOptionsItemSelected(MenuItem item) {  
  50.         // Handle action bar item clicks here. The action bar will  
  51.         // automatically handle clicks on the Home/Up button, so long  
  52.         // as you specify a parent activity in AndroidManifest.xml.  
  53.         int id = item.getItemId();  
  54.         if (id == R.id.action_settings) {  
  55.             return true;  
  56.         }  
  57.         return super.onOptionsItemSelected(item);  
Here it does not produce any output and it is just for illustration purposes. 
 

onCreate()

 
This method creates an activity as shown in the code and it is the very first state as shown in the diagram. This is where we should do all of our initial static set up like create views, bind data to lists and so on. This method is ed a Bundle object containing the activity's previous state. It is always followed by the onStart() method. Note it is not killable by the system.
 

onRestart()

 
This method is called after the activity has been stopped, just prior to it being started again. It is always followed by the onStart() method. Note it is also not killable by the system.
 

onStart()

 
This method is called just before the activity becomes visible to the user. It is followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden. Note it also not killable by the system.
 

onResume()

 
This method is always called just before the activity starts interacting with the user. At this point, the activity is at the top of the activity stack, with the user input going to it. It is always followed by the onPause() method. Note it also not killable by the system.
 

onPause()

 
This method is called when the system is about to start resuming another activity. This method is generally used to commit unsaved changes to persistent data and to stop animations and other things that may be consuming CPU and activities that become halted. It should do whatever it does very quickly because the next activity will not be resumed until it returns. Again this method is followed either by onResume() if the activity returns back to the front or by onStop() if it becomes invisible to the user. Note that it is killable by the system.
 

onStop()

 
This method is always called after the onPause() method and also when the activity is no longer visible to the user. This might happen because it is being destroyed, or because another activity has been resumed and is overwriting it. It is followed either by onRestart() if the activity is returning to interact with the user or by onDestroy() if this activity is going away. Note that it is also killable by the system.
 

onDestroy()

 
This is the last method in the activity lifecycle after an Activity becomes shut down and vanishes from the screen completely. This is the final call that the activity will receive. It would be called either because the activity is finishing by calling the onFinish() method or because the system is temporarily destroying this instance of the activity to save space.
 
Note: All the killing of methods would happen only in extreme circumstances when there is no other resource.
 

Summary

 
This article just tries to explain the Activity life cycle and various method calls involved. This is very important to define Activity because in Android development all the interfaces for a user of what the user sees on the screen are called an Activity and is also a class in Android development.