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

onCreate()

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

onStart()

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

onResume()

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

onPause()

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.

onStop()

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

onDestroy()

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.app.Activity;
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. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(this, “On Create Called In First Activity”, Toast.LENGTH_LONG).show();
Log.i(“FirstActivity”, “Inside onCreate”);
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Toast.makeText(this, “On Start Called In First Activity”, Toast.LENGTH_LONG).show();
Log.i(“FirstActivity”, “Inside onStart”);
}
@Override
protected void onResume()
{
// TODO Auto-generated method stub
super.onResume();

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

Toast.makeText(this, “On Stop Called In First Activity”, Toast.LENGTH_LONG).show();
Log.i(“FirstActivity”, “Inside onStop”);
}
@Override
protected void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
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);
startActivity(intent);
}
}

  •  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=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
>
<TextView
android:id=”@+id/textView1″
android:layout_gravity=”center_horizontal”
android:textSize=”23dp”
android:layout_marginTop=”150dp”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”This Is Fist Activity Activity”
/>

<Button
android:id=”@+id/button1″
android:layout_gravity=”center_horizontal”
android:layout_marginTop=”20dp”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=” Second Activity ”
android:onClick=”startSecondActivity”/>

</LinearLayout>

  •  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=”http://schemas.android.com/apk/res/android”
package=”com.techblogon.activitylifecycleexample”
android:versionCode=”1″
android:versionName=”1.0″ >

<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”17″ />

<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”com.techblogon.activitylifecycleexample.MainActivity”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>

<activity
android:name=”.SecondActivity”>
</activity>

</application>

</manifest>

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.

Tags: