Monday , December 10 2018
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.

LXnx7

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.

 

Example

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:

1) MainActivity.java

package com.example.helloworld;

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

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

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(msg, “The onCreate() event”);
}

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

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

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

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

/** Called just before the activity is destroyed. */
@Override
public void onDestroy() {
super.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:

setContentView(R.layout.activity_main);

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

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

<application
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >

<activity
android:name=”.MainActivity”
android:label=”@string/title_activity_main” >

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

</activity>

</application>
</manifest>

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