Android Activity Lifecycle Example Code Description

Android Activity Lifecycle Example Code Description

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

Learn about Android Activity Lifecycle with Example

Before going to use Android Activity in your application, We need to learn about complete details of an Android Activity Lifecycle. Let go through Android Activity Lifecycle Example Code Description. We will discuss about all real time scenarios and different states of an Android Activity. Once you download the complete sample code example from below, then you can find inline code description for an Android Activity. If you are new to Android Activity, then you might like to read my article Android Fundamentals and Components. Anyway lets have a quick reminder on Android Activity Lifecycle. Below image will give us a overall idea about an Activity’s complete LifeCycle, then we will implement all details in our example.

Android  Activity States With  Description

Android OS manages all Activities in an activity stack. When a new activity is started, it will be placed on the top of the stack and becomes the running activity. Then the previous activity always remains below it in the stack, and will not come to the foreground again until the new activity exits.

In below example, we have taken 2 android activities. So that we can discuss all possible states of an Android Activity. Also we are using Android Toast to display all activity states on the screen. So we need to go through code along with  run the example in device/emulator. Also inline comments are added in the attached example. Logs are also added, so that we can see details in DDMS. You can also debug this application using break points for better understanding.

All Possible states in Android Activity


This method will be call when activity will be created for the first time. This is the starting point of the activity.


This method will be call just before the activity becomes visible on the screen. This state followsonCreate() function.


This method will be call after onStart() method and if the activity is the foreground activity on the screen.


This method will be call when the activity is just about to call another activity so that the current activity has to be paused and the new activity has to be resumed. Here the previous activity is not stopped but it loss the foreground visibility means it goes as background activity. Also if any other application will be on top of the activity, then also the activity will go to this state.


This method is called when your activity is no longer visible on the screen.


This method is called when your current activity has the last chance to do any processing before it is destroyed.

Create Project: Android Activity Lifecycle Example

1. Create a project with project name: ActivityLifeCycleExample

2. Fill Application Name: ActivityLifeCycleExample

3. Fill the Package Name as: com.techblogon.activitylifecycleexample

4. I have used SDK version Android 4.0.3 and Eclipse Version Indigo. But you can use any version.

5. Add below 2 java files (MainActivity.Java and SecondActivity.Java) in you project’s src folder.

  • MainActivity.Java

package com.techblogon.activitylifecycleexample;

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

public class MainActivity extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState)
Toast.makeText(this, “On Create Called In First Activity”, Toast.LENGTH_LONG).show();
Log.i(“FirstActivity”, “Inside onCreate”);
protected void onStart() {
// TODO Auto-generated method stub
Toast.makeText(this, “On Start Called In First Activity”, Toast.LENGTH_LONG).show();
Log.i(“FirstActivity”, “Inside onStart”);
protected void onResume()
// TODO Auto-generated method stub

Toast.makeText(this, “On Resume Called In First Activity”, Toast.LENGTH_LONG).show();
Log.i(“FirstActivity”, “Inside onResume”);
protected void onPause() {
// TODO Auto-generated method stub
Toast.makeText(this, “On Pause Called In First Activity”, Toast.LENGTH_LONG).show();
Log.i(“FirstActivity”, “Inside onPause”);
protected void onStop() {
// TODO Auto-generated method stub

Toast.makeText(this, “On Stop Called In First Activity”, Toast.LENGTH_LONG).show();
Log.i(“FirstActivity”, “Inside onStop”);
protected void onDestroy()
// TODO Auto-generated method stub
Toast.makeText(this, “On Destroy Called In First Activity”, Toast.LENGTH_LONG).show();
Log.i(“FirstActivity”, “Inside onDestroy”);

public void startSecondActivity(View V)
// create an new Intent and Start Second Activity
Intent intent=new Intent(this,SecondActivity.class);

  •  SecondActivity.Java

6. Add below 2 xml layout files (main.xml and layout2.xml) in your project’s res/layout folder.

  • main.xml

<LinearLayout xmlns:android=””
android:text=”This Is Fist Activity Activity”

android:text=” Second Activity ”


  •  layout2.xml

7. Add the below Manifest file (AndroidManifest.xml) in your project’s root folder.

  • AndroidManifest.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=””
android:versionName=”1.0″ >

android:targetSdkVersion=”17″ />

android:theme=”@style/AppTheme” >
android:label=”@string/app_name” >
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />




8. Build and Run the application, then observe all states of an android activity. You can see a Android Toast of each changed android states.

Note: Also you can download the complete code and import the project as an Existing android project.

Leave a Reply

Your email address will not be published. Required fields are marked *