Activities and Intents Tutorial: Activity

PrevMainNext

Activity: Definition

To put it in simple words, Activities form the lifeline of an Android application. It is the component which facilitates interaction between an end user and the application itself. From a UI perspective, Activity refers to the area of the screen where the user can do certain action in order to do a specific action e.g. make a call or send a text message.

Since activities are the visual backbone of an Android application, these serve as the user input/output mechanism – fetching input from the user and displaying output to the user.

Android Application and Activities

An activity broadly maps one to one to a screen in Android application. Each screen in turn is comprised of a certain number of UI elements such as buttons, text, images, maps etc. Since most applications would have multiple screens, therefore it’s reasonable to assume that an Android application would usually have multiple activities.

Main Activity

Each application always has a main activity which serves as the starting point for the application. This usually refers to the initial screen that appears when the application is started by the user.

In the Hello World example covered in Module 1, the HelloWorldActivity served as the main activity of the application. It extends the android.app.Activity class.

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class HelloWorldActivity extends Activity {

In most cases, the main activity would then launch another activity in order to enable the user to perform specific actions.

Passing Control between Activities

Each activity serves a definite purpose and when that has been achieved, it must pass control of the app by starting another activity in order to perform different actions. When new activity starts, it halts the previous activity and it gets stored on the system stack (referred to as the “back stack”).

Since the stack is a LIFO (Last-In-First-Out) structure, any new activity gets pushed on to the end of the stack.

Callbacks

Activity transitions in an application are handled through callback methods. Whenever activity state changes, developers can perform any action on the transition using callbacks.

How to Create an Activity?

As mentioned earlier, an activity is created by extending the android.app.Activity class. Each activity must implement the onCreate() and onPause() methods.

With reference to the Hello World example covered in Module 1, the onCreate() method code of HelloWorldActivity is given below.

public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	TextView tv = new TextView(this);
	tv.setText("Hello, Android");
	setContentView(tv);       
}

This essentially means that when the HelloWorldActivity is created, the onCreate() method is called and it executes the mentioned code to display the “Hello Android” label.

The onCreate() method has one argument – a bundle object which saves the activity’s earlier state.

Activity Lifecycle

The following figure illustrates the Android Activity Lifecycle for an application.

the Android Activity Lifecycle for an application

As mentioned earlier, the first step is the onCreate() method. This is typically the initialization phase for an activity. It is followed by the onStart() method which is called just before the activity UI becomes visible. Depending on whether the UI is shown or hidden, the onResume() or onStop() method is subsequently called.

From a UI perspective, the onResume() method is the last method called before the activity UI initiates user interaction. When the activity calls another activity, it’s onPause() method is called. For apps which have multiple activities, it is recommended to use the onPause() method to save any transient data as well as kill any CPU intensive tasks which might affect the overall user experience.

Once an activity UI is not visible to user, it triggers the onStop() method for that activity. This typically happens when an activity passes control to another activity for user interaction. Once the activity is stopped, it can be restarted by calling the onRestart() method or it’s completely destroyed thereby calling the onDestroy() method.

Here’s an example Activity class with all the activity lifecycle methods described above.

package com.learncomputer;

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

public class Module2Activity extends Activity {
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//Called when activity is first created
	}

	@Override
	protected void onStart() {
		super.onStart();
		// Called when activity is about to become visible.
	}

	@Override
	protected void onResume() {
		super.onResume();
		// Called when activity has become visible
	}

	@Override
	protected void onPause() {
		super.onPause();
		// Called when activity is passing control to another activity
	}

	@Override
	protected void onStop() {
		super.onStop();
		// Called when activity is no longer visible
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// Called when activity is about to be destroyed.
	}
} 
PrevMainNext