Skip to content

Android - Activities

An Activity represents a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View). While activities are often presented to the user as full-screen windows, they can also be used in other ways: as floating windows or embedded inside another activity.

An Android Activity is typically a subclass of androidx.appcompat.app.AppCompatActivity for modern apps, providing compatibility with older Android versions and access to features like Material Design theming.

Similar to how a C/C++ or Java program starts with a main() function, the Android system initiates code in an Activity instance by invoking specific callback methods that correspond to its lifecycle stages. The Activity lifecycle is a sequence of states an Activity goes through, from when it’s created to when it’s destroyed. These states are:

  • Created: onCreate() - The Activity is being created. This is where you should perform basic application startup logic that should happen only once for the entire life of the activity (e.g., create views, bind data to lists).
  • Started: onStart() - The Activity is becoming visible to the user.
  • Resumed: onResume() - The Activity is now in the foreground and ready for user interaction.
  • Paused: onPause() - The Activity is partially obscured or is no longer the focus of user interaction, but is still visible. Release resources that should not be held while paused (e.g., camera, sensors). This is often the first indication that the user is leaving your activity.
  • Stopped: onStop() - The Activity is no longer visible to the user. Perform more significant, CPU-intensive shutdown operations here. The Activity may be destroyed or restarted.
  • Destroyed: onDestroy() - The Activity is being destroyed. This can happen either because the activity is finishing (by calling finish()) or because the system is temporarily destroying this instance of the activity to save space. Perform final cleanup of all resources.

Additionally, onRestart() is called when an activity that was previously stopped is being started again.

It’s crucial to understand and implement these lifecycle callbacks correctly to ensure your app behaves as users expect, manages resources efficiently, and handles interruptions gracefully (e.g., incoming calls, screen rotations, or the system reclaiming resources).

No.Callback & Description
1onCreate(Bundle savedInstanceState)
Called when the activity is first created. This is where you should do all of your normal static set up: create views, bind data to lists, etc. This method also provides you with a Bundle containing the activity’s previously frozen state, if there was one.
2onStart()
Called when the activity is becoming visible to the user. Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.
3onResume()
Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it.
4onPause()
Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns.
5onStop()
Called when the activity is no longer visible to the user. This may happen because it is being destroyed, or because another activity (either an existing one or a new one) has been resumed and is covering it. Followed by either onRestart() if this activity is coming back to interact with the user, or onDestroy() if this activity is going away.
6onRestart()
Called after your activity has been stopped, prior to it being started again. Always followed by onStart().
7onDestroy()
Called before the activity is destroyed. This is the final call that the activity will receive. This can happen either because the activity is finishing (someone called finish() on it), or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

This example demonstrates how to log Activity lifecycle events. Create a new Android project in Android Studio using Kotlin or Java.

Modify your main activity file (e.g., MainActivity.java or MainActivity.kt):

// MainActivity.java (using Java)
package com.example.app;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivityLifecycle";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate() called");
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart() called");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume() called");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause() called");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop() called");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "onRestart() called");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy() called");
}
}

The setContentView(R.layout.activity_main); line loads the UI layout from res/layout/activity_main.xml.

Every activity used in your application must be declared in the AndroidManifest.xml file. The main activity (the entry point of your app) must be declared with an <intent-filter> that includes the MAIN action and LAUNCHER category:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyAppTheme">
<activity
android:name=".MainActivity"
android:exported="true"> <!-- Required for Android 12+ if it has intent-filter -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Declare other activities here -->
</application>
</manifest>

If the MAIN action or LAUNCHER category are not declared for at least one activity, your app icon will not appear in the device’s app launcher.

Run the application on an emulator or device. Open the Logcat window in Android Studio (View > Tool Windows > Logcat) and filter by the tag MainActivityLifecycle (or your chosen tag). You’ll see logs for onCreate(), onStart(), and onResume() when the app launches.

Typical Logcat output on launch:

D/MainActivityLifecycle: onCreate() called
D/MainActivityLifecycle: onStart() called
D/MainActivityLifecycle: onResume() called

Now, try these actions and observe the Logcat output:

  • Press the Home button: onPause(), onStop() will be called.
  • Re-open the app from recent apps: onRestart(), onStart(), onResume() will be called.
  • Rotate the screen (if auto-rotate is enabled): The activity is typically destroyed and recreated. You’ll see onPause(), onStop(), onDestroy(), then onCreate(), onStart(), onResume(). (To handle configuration changes like rotation without full recreation, or to save state, you’d use onSaveInstanceState(Bundle) and potentially ViewModels from Jetpack.)
  • Press the Back button: onPause(), onStop(), onDestroy() will be called as the activity finishes.

Understanding the activity lifecycle is crucial for building responsive and resource-efficient Android applications. For managing UI-related data across configuration changes (like screen rotations) and other lifecycle events, consider using ViewModel from the Android Jetpack library. This helps separate UI logic from data handling and prevents data loss during lifecycle events.