Saturday , February 23 2019
Home / Android / Android Activity Lifecycle

Android Activity Lifecycle

An activity represents a single screen with a user interface just like window or frame of Java. Android activity is the subclass of ContextThemeWrapper class.

An Android Activity has a Lifecycle during which it performs a few things. The first question when reading about Lifecycle of Activity Lifecycle is – Why is it required ? Let’s understand it with an example.

Assume you are playing a game on your phone, you are at level 2 and suddenly some one calls you. When you get a call, your games stop and you see the Caller Id Screen. When you resume your game after the call ends, it gets resumed from the same point where you left it.

If you have worked with C, C++ or Java programming language then you must have seen that your program starts from main() function. Very similar way, Android system initiates its program with in an Activity starting with a call ononCreate() callback method. There is a sequence of callback methods that start up an activity and a sequence of callback methods that tear down an activity as shown in the below Activity life cycle diagram.


The Activity class defines the following call backs i.e. events. You don’t need to implement all the callbacks methods. However, it’s important that you understand each one and implement those that ensure your app behaves the way users expect.


Callback Description
onCreate() This is the first callback and called when the activity is first created.
onStart() This callback is called when the activity becomes visible to the user.
onResume() This is called when the user starts interacting with the application.
onPause() The paused activity does not receive user input and cannot execute any code and called when the current activity is being paused and the previous activity is being resumed.
onStop() This callback is called when the activity is no longer visible.
onDestroy() This callback is called before the activity is destroyed by the system.
onRestart() This callback is called when the activity restarts after stopping it.



This example will take you through simple steps to show Android application activity life cycle.The Log.d() method has been used to generate log messages:


package com.example.helloworld;

import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {
String msg = “Android : “;

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
Log.d(msg, “The onCreate() event”);

/** Called when the activity is about to become visible. */
protected void onStart() {
Log.d(msg, “The onStart() event”);

/** Called when the activity has become visible. */
protected void onResume() {
Log.d(msg, “The onResume() event”);

/** Called when another activity is taking focus. */
protected void onPause() {
Log.d(msg, “The onPause() event”);

/** Called when the activity is no longer visible. */
protected void onStop() {
Log.d(msg, “The onStop() event”);

/** Called just before the activity is destroyed. */
public void onDestroy() {
Log.d(msg, “The onDestroy() event”);

2) An activity class loads all the UI component using the XML file available inres/layout folder of the project. Following statement loads UI components fromres/layout/activity_main.xml file:


3) An application can have one or more activities without any restrictions. Every activity you define for your application must be declared in yourAndroidManifest.xml file and the main activity for your app must be declared in the manifest with an <intent-filter> that includes the MAIN action and LAUNCHER category as follows:

<manifest xmlns:android=””
android:versionName=”1.0″ >

android:targetSdkVersion=”22″ />

android:theme=”@style/AppTheme” >

android:label=”@string/title_activity_main” >

<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER”/>



About admin

Check Also

Android Activity Launch Modes

Launch mode is an instruction for Android OS which specifies how the activity should be ...

Leave a Reply