Android Activities

Android Activities

If you are familiar with Object Oriented Programming languages which include C, C++, Java, and a few others then you must be aware that every program starts with main() function. Similarly, when we talk about an Android program then the initiation takes place with the launched Activity. Here, the first method to be called is onCreate() callback method.

Android Activities Cycle
Also Read: Android Resources

In the provided flow chart, you can easily see that a series of callback methods (onCreate() method -> onStart() method -> onResume() method) initiate an Activity. While another series of callback methods (onPause() method -> onStop() method -> onDestroy() method) is responsible for shutting down the same Activity. The above Activity life cycle diagram will help you understand every phase of an Activity Life Cycle.

Well, an Activity class contains the definitions for various callbacks which are known as events. These events are tabulated in the table provided below. So, because of these events, you don’t need to implement all the callback methods every now and then. But, it is really important that you understand the functioning and implementation of each and every callback method. With the help of this knowledge, you will be able to make sure that your application behaves according to the expectation of the end users.

S. No. Callback Method Description
1 onCreate() * This is the first callback method in the Activity Life Cycle.
* This method is called whenever an Activity creation takes place.
2 onStart() * This callback method comes into existence when an Activity becomes visible to an end user.
3 onResume() * This method gets called whenever a user begins its interaction with the application.
4 onPause() * This method doesn’t accept inputs from the user as the functioning of the program gets paused.
* At the paused phase, the program doesn’t even execute the code.
* This callback method is called whenever the program pauses and the last activity gets resumed.
5 onStop() * This method is called whenever the activity is no more visible to the user.
6 onDestroy() * This callback method is called every time before the activity needs to be destroyed by the system.
7 onRestart() * This method is called when the activity needs to be resumed after it has been stopped once.

For an instance, follow the given steps for the better understanding of an Android application activity life cycle. These steps will lead you to make a few changes in the already discussed Hello World application.

Step 1: You need to create an Android application on the Android Studio. Name this application as HelloWorld, which will be placed under the package com.example.helloworld. This has been already mentioned in the chapter, Hello World Example.

Step 2: Now, you need to modify the main activity file, which is file. But make sure that you leave the rest of the files as it is. You can see these changes happening in the below section.

Step 3: At the final step, run the newly created application so that the Android Emulator gets launched. After this, check for the final output after the changes have been implemented on the application.

Below is the content present in the modified main activity file. You can locate this file at src/com.example.helloworld/ Also, you must be aware of the fact that this file contains all the basic activity lifecycle methods.

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”);

Here, Log.d() method is used to generate log messages.


Well, an Activity class loads various UI components with the help of available XML file. This XML file can be found under the folder res/layout. The below command is responsible for the loading of the UI components from the file res/layout/activity_main.xml.


It is possible that a single application can have more than one activities without any constraints applied. But, on the other hand, it is important that you define each and every activity of your Android application in the AndroidManifest.xml file. You also need to declare the main activity in the manifest file. This declaration needs to be done with the help of the <intent-filter>. This <intent-filter> contains MAIN action and the LAUNCHER category too. You can clearly see this in the content of the following manifest file.

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=””

<activity android:name=”.MainActivity”>
<action android:name=”android.intent.action.MAIN” />

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


In any case, if you miss declaring either the MAIN action or the LAUNCHER category for one of your application’s activities then it is definite that your application icon won’t appear on the home screen of the system.

Now, let’s run this modified Hello World! application. Here, we are assuming that you have already created the AVD during the process of environment setup. As mentioned earlier, we will be using Android Studio to run our application. So, after you run the Android Studio, open any of the activity files of your project and hit on the Run icon from the toolbar of the Android Studio. If everything is in place then you will be displayed with an Emulator window. On this window, you can see the log messages generated by the application. These log messages will be displayed on the LogCat window on the Android Studio.

08-23 10:32:07.682 4480-4480/com.example.helloworld D/Android :: The onCreate() event
08-23 10:32:07.683 4480-4480/com.example.helloworld D/Android :: The onStart() event
08-23 10:32:07.685 4480-4480/com.example.helloworld D/Android :: The onResume() event

Now, locate the lock screen button on the Emulator. Once, you find it, click on the button to generate the below-provided event messages. These event messages will get displayed again on the LogCat window.

08-23 10:32:53.230 4480-4480/com.example.helloworld D/Android :: The onPause() event
08-23 10:32:53.294 4480-4480/com.example.helloworld D/Android :: The onStop() event

After this, try unlocking the screen. It will generate the following event messages on the LogCat window.

08-23 10:34:41.390 4480-4480/com.example.helloworld D/Android :: The onStart() event
08-23 10:34:41.392 4480-4480/com.example.helloworld D/Android :: The onResume() event

At last, try clicking on the Back button on the Emulator. And the following event messages will be generated as a result of that.

08-23 10:37:24.806 4480-4480/com.example.helloworld D/Android :: The onPause() event 08-23 10:37:25.668 4480-4480/com.example.helloworld D/Android :: The onStop() event 08-23 10:37:25.669 4480-4480/com.example.helloworld D/Android :: The onDestroy() event

With this, the whole Activity Life Cycle of an Android application completes itself.