In Android, an activity is referred to one screen in an application. It is very similar to a single-window of any desktop application. An Android app consists of one or more screens or activities.
Each activity goes through various stages or a lifecycle and is managed by activity stacks. So when a new activity starts, the previous one always remains below it. There are four stages of an activity.
- If an activity is in the foreground of the screen i.e at the top of the stack, then it is said to be active or running. This is usually the activity that the user is currently interacting with.
- If an activity has lost focus and a non-full-sized or transparent activity has focused on top of your activity. In such a case either another activity has a higher position in multi-window mode or the activity itself is not focusable in current window mode. Such activity is completely alive..
- If an activity is completely hidden by another activity, it is stopped or hidden. It still retains all the information, and as its window is hidden thus it will often be killed by the system when memory is needed elsewhere.
- The system can destroy the activity from memory by either asking it to finish or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.
For each stage, android provides us with a set of 7 methods which have their own significance for each stage in the life cycle. The image shows a path of migration whenever an app switches from one state to another.
Detailed introduction on each of the method is stated as follows:
- onCreate():It is called when the activity is first created. This is where all the static work is done like creating views, binding data to lists, etc. This method also provides a Bundle containing its previous frozen state, if there was one.
Example:packagecom.example.piyush.myapplication;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.widget.Toast;publicclassMainActivityextendsAppCompatActivity {@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);// Bundle containing previous frozen statesetContentView(R.layout.activity_main);// The content view pointing to the id of layout// in the file activity_main.xmlToast toast= Toast.makeText(getApplicationContext(),"onCreate Called",Toast.LENGTH_LONG).show();}} - onStart():It is invoked when the activity is visible to the user. It is followed by onResume() if activity is invoked from background. It is also invoked after onCreate() when activity is first started.
Example:packagecom.example.piyush.myapplication;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.widget.Toast;publicclassMainActivityextendsAppCompatActivity {@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);// Bundle containing previous frozen statesetContentView(R.layout.activity_main);// The content view pointing to the id of layout// in the file activity_main.xmlToast toast= Toast.makeText(getApplicationContext(),"onCreate Called",Toast.LENGTH_LONG).show();}protectedvoidonStart(){// It will show a message on the screen// then onStart is invokedToast toast= Toast.makeText(getApplicationContext(),"onStart Called",Toast.LENGTH_LONG).show();}} - onRestart():It is invoked after the activity has been stopped and prior to its starting stage and thus is always followed by onStart() when any activity is revived from background to on screen.
Example:packagecom.example.piyush.myapplication;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.widget.Toast;publicclassMainActivityextendsAppCompatActivity {@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);// Bundle containing previous frozen statesetContentView(R.layout.activity_main);// The content view pointing to the id of layout// in the file activity_main.xmlToast toast= Toast.makeText(getApplicationContext(),"onCreate Called",Toast.LENGTH_LONG).show();}protectedvoidonRestart(){// It will show a message on the screen// then onRestart is invokedToast toast= Toast.makeText(getApplicationContext(),"onRestart Called",Toast.LENGTH_LONG).show();}} - onResume():It is invoked when the activity starts interacting with the user. At this point, the activity is at the top of the activity stack, with a user interacting with it. Always followed by onPause() when the activity goes into the background or is closed by the user.
Example:packagecom.example.piyush.myapplication;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.widget.Toast;publicclassMainActivityextendsAppCompatActivity {@OverrideprotectedvoidonCreate(Bundle savedInstanceState){// Bundle containing previous frozen statesuper.onCreate(savedInstanceState);// The content view pointing to the id of layout// in the file activity_main.xmlsetContentView(R.layout.activity_main);Toast toast= Toast.makeText(getApplicationContext(),"onCreate Called",Toast.LENGTH_LONG).show();}protectedvoidonResume(){// It will show a message on the screen// then onResume is invokedToast toast= Toast.makeText(getApplicationContext(),"onResume Called",Toast.LENGTH_LONG).show();}} - onPause(): It is invoked when an activity is going into the background but has not yet been killed. It is a counterpart to onResume(). When an activity is launched in front of another activity, this callback will be invoked on the top activity (currently on screen). The activity, under the active activity, will not be created until the active activity’s onPause() returns, so it is recommended that heavy processing should not be done in this part.
Example:
packagecom.example.piyush.myapplication;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.widget.Toast;publicclassMainActivityextendsAppCompatActivity {@OverrideprotectedvoidonCreate(Bundle savedInstanceState){// Bundle containing previous frozen statesuper.onCreate(savedInstanceState);// The content view pointing to the id of layout// in the file activity_main.xmlsetContentView(R.layout.activity_main);Toast toast= Toast.makeText(getApplicationContext(),"onCreate Called",Toast.LENGTH_LONG).show();}protectedvoidonPause(){// It will show a message on the screen// then onPause is invokedToast toast= Toast.makeText(getApplicationContext(),"onPause Called",Toast.LENGTH_LONG).show();}} - onStop(): It is invoked when the activity is not visible to the user. It is followed by onRestart() when the activity is revoked from the background, followed by onDestroy() when the activity is closed or finished, and nothing when the activity remains on the background only. Note that this method may never be called, in low memory situations where the system does not have enough memory to keep the activity’s process running after its onPause() method is called.
Example:packagecom.example.piyush.myapplication;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.widget.Toast;publicclassMainActivityextendsAppCompatActivity {@OverrideprotectedvoidonCreate(Bundle savedInstanceState){// Bundle containing previous frozen statesuper.onCreate(savedInstanceState);// The content view pointing to the id of layout// in the file activity_main.xmlsetContentView(R.layout.activity_main);Toast toast= Toast.makeText(getApplicationContext(),"onCreate Called",Toast.LENGTH_LONG).show();}protectedvoidonStop(){// It will show a message on the screen// then onStop is invokedToast toast= Toast.makeText(getApplicationContext(),"onStop Called",Toast.LENGTH_LONG).show();}} - onDestroy():
The final call received before the activity is destroyed. This can happen either because the activity is finishing (when finish() is invoked) or because the system is temporarily destroying this instance of the activity to save space. To distinguish between these scenarios, check it with isFinishing() method.Example:packagecom.example.piyush.myapplication;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.widget.Toast;publicclassMainActivityextendsAppCompatActivity {@OverrideprotectedvoidonCreate(Bundle savedInstanceState){// Bundle containing previous frozen statesuper.onCreate(savedInstanceState);// The content view pointing to the id of layout// in the file activity_main.xmlsetContentView(R.layout.activity_main);Toast toast= Toast.makeText(getApplicationContext(),"onCreate Called",Toast.LENGTH_LONG).show();}protectedvoidonDestroy(){// It will show a message on the screen// then onDestroy is invokedToast toast= Toast.makeText(getApplicationContext(),"onDestroy Called",Toast.LENGTH_LONG).show();}}
Demo Android app to demonstrate Activity Lifecycle in Android:
package com.example.piyush.myapplication; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toast toast = Toast.makeText( getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG) .show(); } protected void onStart() { super.onStart(); Toast toast = Toast.makeText( getApplicationContext(), "onStart Called", Toast.LENGTH_LONG) .show(); } @Override protected void onRestart() { super.onRestart(); Toast toast = Toast.makeText( getApplicationContext(), "onRestart Called", Toast.LENGTH_LONG) .show(); } protected void onPause() { super.onPause(); Toast toast = Toast.makeText( getApplicationContext(), "onPause Called", Toast.LENGTH_LONG) .show(); } protected void onResume() { super.onResume(); Toast toast = Toast.makeText( getApplicationContext(), "onResume Called", Toast.LENGTH_LONG) .show(); } protected void onStop() { super.onStop(); Toast toast = Toast.makeText( getApplicationContext(), "onStop Called", Toast.LENGTH_LONG) .show(); } protected void onDestroy() { super.onDestroy(); Toast toast = Toast.makeText( getApplicationContext(), "onDestroy Called", Toast.LENGTH_LONG) .show(); } } |
Output:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.


