Android Activity Lifecycle Example Code Description

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. If you want to know about How to Debug an Android Application  then click here.

android activity lifecycle example


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 follows onCreate() 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

  •  SecondActivity.Java

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

  • main.xml

  •  layout2.xml

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

  • AndroidManifest.xml

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.

downloadDownload Android Activity Lifecycle Example Code Description from Here.

I hope this tutorial can help you at it’s best.


Post By SmrutiRanjan (57 Posts)

Working @ Samsung as a Project Lead for Android Smartphones. I have been blogging since 2008. Previously I was writing articles for other bloggers, but finally I have started my own blog-"Techblogon".I am also an active contributor for the blog-"Gadgets n Gizmos World". Job is my necessity, but blogging is my passion.

Website: → Techblogon


9 Responses to Android Activity Lifecycle Example Code Description

  1. Sant Ram says:

    Awesome work!!!! thanks to SmrutiRanjan.

  2. hassan says:

    in Error is :

    MainActivity.Java – setContentView(R.layout.main);

    SecondActivity.Java – setContentView(R.layout.layout2);

    Can yo Please help me to rectify these errors ….

  3. hassan says:

    R’ error will showing if i importing ” import android.R; ” then it will show the error oN “” R.layout.main “” Main “”

  4. dhar says:

    Thanks for the post but what about onRestart() and onFreeze(),can you please explain those scenarios also.

  5. precursor says:

    Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! By the way, how can we communicate?

Leave a Reply

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

7 − two =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">