Activity and its lifecycle

Android applications or apps are made up of Activities. The Android Activity class android.app.Activity is core to any Android application. Typically, one Activity is displayed at a time and this Activity occupies the entire screen. Each Activity is made up of views, which are the most basic building block of a user interface. The View is a base class for objects that interact with the user; they are called widgets (TextView, EditText, ImageView, Button, etc.).

In this tutorial we'll discuss the common terms used in Android UI development: activities, layouts, views, and widgets.

Activities

An Android activity represents a screen with which a user can interact. The Activity class is used to respond to user input. An activity can transition to another activity as the user navigates between screens. Any component that is drawn to the screen lives within the bounds of an activity but Activity class itself does not draw anything. A Java source file represents an activity class in app/java/. A very simple activity looks like:

package com.brainbell.myfirstapp;

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

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  /*Here, you're telling your Activity to load
  the activity_main.xml layout resource file*/  
  
  setContentView(R.layout.activity_main);

 }
}

An activity class loads its user interface (UI) component (or elements) using the XML file defined in app/res/layout folder. In this example, we would load the UI from the activity_main.xml file:

setContentView(R.layout.activity_main);

There is no main function defined for Android applications so the Android system launches applications with the defined launcher activity, which serves as the entry point for the application. When the application is launched, the Android operating system application manager uses the Main Activity to start the application. The Main Activity is the first activity users see.

Every instance of Activity has a lifecycle. During this lifecycle, an activity transitions between various states. For each transition, there is an event that notifies the activity of the change in its state.

Activity lifecycle

Android activities have a defined lifecycle to manage application runtime from launch to the end of application life. The Activity base class defines a series of events that govern the life cycle of an activity.

Activity LifeCycle
Official diagram explaining the activity lifecycle from : https://developer.android.com/reference/android/app/Activity.html. The Activity class defines the following events:

  • onCreate() : Called when the activity is first created. This is a place where we usually do main UI elements initialization.

  • onStart() : Called when the activity becomes visible to the user

  • onResume() : Called when the activity starts interacting with the user. At this stage we can start any services or code that needs to run while your activity is in the foreground.

  • onPause() : Called when the current activity is being paused and the previous activity is being resumed. This is a good place to save all the information you will need when you resume again. If there are any unsaved changes, you should save them here. At this stage we can stop any services or code that does not need to run when your activity is not in the foreground.

  • onStop() : Called when the activity is no longer visible to the user

  • onDestroy() : Called before the activity is destroyed by the system. At this stage we should free up resources before the activity is destroyed.

  • onRestart() : Called when the activity has been stopped and is restarting again. For example, you turn off your phone screen (lock it) and then unlock it again.

By default, the activity created for you contains only the onCreate() event which helps the Activity to display the UI elements. To understand the various stages of the activity, we'll implement all the events, and define our own log messages for each event, and then subject the activity to various user interactions.

Create a new Android project and in the MainActivity.java file, add the following code:

package com.brainbell.myfirstapp;

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

public class MainActivity extends Activity {

 private static final String TAG = "Lifecycle";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  Log.d(TAG, "onCreate: ");
 }

 @Override
 protected void onStart() {
  super.onStart();
	Log.d(TAG, "onStart: ");
 }

 @Override
 protected void onRestart() {
  super.onRestart();
  Log.d(TAG, "onRestart: ");
 }

 @Override
 protected void onResume() {
  super.onResume();
  Log.d(TAG, "onResume: ");
 }

 @Override
 protected void onPause() {
  super.onPause();
  Log.d(TAG, "onPause: ");
 }

 @Override
 protected  void onStop() {
  super.onStop();
  Log.d(TAG, "onStop: ");
 }

 @Override
 protected void onDestroy() {
  super.onDestroy();
  Log.d(TAG, "onDestroy: ");
 }
}

Run your app on virtual or real device (read How to setup emulator to run virtual device and Run your app on a real Android device).

Open Logcat window from the main menu View > Tool Windows > Logcat or click Logcat tool button from the bottom of Android studio. Select your device from the logcat menu and filter logs by searching the TAG value: Lifecycle, as shown in following image.

LogCat

When the activity is first loaded, you should see something very similar to the following in the logcat console:

11-13 01:12:16.600 ... D/Lifecycle: onCreate: 
11-13 01:12:16.600 ... D/Lifecycle: onStart: 
11-13 01:12:16.600 ... D/Lifecycle: onResume

If you click the Back button on the Android emulator, you see the following:

11-13 01:12:34.140 ... D/Lifecycle: onPause: 
11-13 01:12:34.620 ... D/Lifecycle: onStop: 
11-13 01:12:34.620 ... D/Lifecycle: onDestroy:

Above example shows, an activity is destroyed when we click the Back button and whatever state the activity is currently in will be lost.

Click the app icon on your (virtual or real) device to execute it again and observe the following:

11-13 01:13:09.150 ... D/Lifecycle: onCreate: 
11-13 01:13:09.150 ... D/Lifecycle: onStart: 
11-13 01:13:09.150 ... D/Lifecycle: onResume:

Click the Home button on your (virtal or real) deivce and then click on another app so that the activity is pushed to the background. Observe the output in the logcat window:

11-13 01:13:18.940 ... D/Lifecycle: onPause: 
11-13 01:13:19.420 ... D/Lifecycle: onStop:

Notice that the onDestroy() event is not called, indicating that the activity is still in memory. Exit from the "another app" by clicking the Back button. Now click your app icon, the activity is now visible again. Observe the output in the logcat window:

11-13 01:13:22.990 ... D/Lifecycle: onRestart:
11-13 01:13:22.990 ... D/Lifecycle: onStart:
11-13 01:13:22.990 ... D/Lifecycle: onResume:

The onRestart() event is now fired, followed by the onStart() and onResume() methods.

Conclusion

  1. When an activity is created for the first time, the onCreate() method is called.

  2. When an activity is started, the onStart() and onResume() methods are always called, regardless of whether the activity is restored from the background or newly created.

  3. The onPause() method is called when an activity is sent to the background or when a user kills an activity by tapping the Back button

  4. An activity is destroyed when we click the Back button. It is important to understand that whatever state the activity is currently in will be lost. So you need to write some logic in your activity to preserve its state when the activity is destroyed

by BrainBellupdated