Activities Life Cycle in Android

Introduction

 
An Activity is a sub-container within an application container that describes all the UI controls.
 
Every Container has a life cycle (methods) that determine the phase's Activity.
 
To create an Activity in Android we have to extend the Activity class.
 

How to work with an Activity?

 
An Image view of an Activity's life cycle:
 
LifeCycle.jpg
 
The following is the source code.
 
MainActivity.java
  1. package com.Saving.savingactivity;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. //import android.view.Menu;  
  5. import android.widget.Toast;  
  6. public class MainActivity extends Activity {  
  7.       @Override  
  8.       protected void onRestoreInstanceState(Bundle savedInstanceState) {  
  9.             super.onRestoreInstanceState(savedInstanceState);  
  10.             Toast.makeText(getBaseContext(), "Restored ", Toast.LENGTH_LONG).show();  
  11.       };  
  12.       @Override  
  13.       protected void onStart() {  
  14.             // TODO Auto-generated method stub  
  15.             super.onStart();  
  16.             Toast.makeText(getBaseContext(), "Started ", Toast.LENGTH_LONG).show();  
  17.       }  
  18.       @Override  
  19.       protected void onResume() {  
  20.             // TODO Auto-generated method stub  
  21.             super.onResume();  
  22.             Toast.makeText(getBaseContext(), "Resumed ", Toast.LENGTH_LONG).show();  
  23.       }  
  24.       @Override  
  25.       protected void onPause() {  
  26.             // TODO Auto-generated method stub  
  27.             super.onPause();  
  28.             Toast.makeText(getApplicationContext(), "Paused ", Toast.LENGTH_LONG).show();  
  29.       }  
  30.       @Override  
  31.       protected void onSaveInstanceState(Bundle outState) {  
  32.             // TODO Auto-generated method stub  
  33.             super.onSaveInstanceState(outState);  
  34.             Toast.makeText(getApplicationContext(), "Saved ", Toast.LENGTH_LONG).show();  
  35.       }  
  36.       @Override  
  37.       protected void onStop() {  
  38.             // TODO Auto-generated method stub  
  39.             super.onStop();  
  40.             Toast.makeText(getApplicationContext(), "Stoped ", Toast.LENGTH_LONG).show();  
  41.       }  
  42.       @Override  
  43.       protected void onDestroy() {  
  44.             // TODO Auto-generated method stub  
  45.             super.onDestroy();  
  46.             Toast.makeText(getApplicationContext(),"Destroy ", Toast.LENGTH_LONG).show();  
  47.       }  
  48. } 
Activity_main.xml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity"  
  6.     android:layout_centerHorizontal="true"  
  7.      android:layout_centerVertical="true">   
  8.     <TextView  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="@string/hello_world" />  
  12. </LinearLayout>  
Step 1
 
First of all, an Activity is creates on a pre-defined Layout by the "onCreate" method.
 
Step 2
 
 After that, the Activity will start in the "onStart" method.
 
onstart.jpg
 
Step 3
 
After starting the "onResume" method displays your current UI.
 
resumed.jpg
 
Step 4
 
After "onResume" if we kill or auto kills the UI then the UI calls the "onPause" method.
 
paused.jpg
 
Step 5
 
After "onPause" for the current UI, the UI call goes to the "onSave" method.
 
saved.jpg
 
Step 6
 
The "onSave" method may be called after "onPause" or "onStop"; it depends on the condition in which the UI is killed.
 
stoped.jpg
 
Step 7
 
If we kill the process or active UI then that goes directly to destroy (without saving the current UI ) by the "onDestroy" method.
 
Otherwise, it goes to "onStop" (step 4).
 
destroy.jpg


Similar Articles