Android Activity Lifecycle with Example Code

Posted By on March 23, 2016


Download PDF
Android Fundamentals and Components
Android Intent

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.

life-cycle-of-an-activity-in-android

 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

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
  •  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.

Android Fundamentals and Components
Android Intent

Download PDF

Posted by Akash Kurup

Founder and C.E.O, World4Engineers Educationist and Entrepreneur by passion. Orator and blogger by hobby

Website: http://world4engineers.com