Deep Dive into Android Activity Lifecycle: From Creation to Destruction

Nov 21, 2025 · Programming · 13 views · 7.8

Keywords: Android | Activity Lifecycle | onCreate | onResume | onDestroy | State Management | Resource Optimization

Abstract: This article provides an in-depth exploration of the seven core methods in the Android Activity lifecycle: onCreate(), onStart(), onResume(), onPause(), onStop(), onRestart(), and onDestroy(). By analyzing the invocation timing, functional responsibilities, and best practices of each method, combined with practical call sequences in common user interaction scenarios (such as app launch, incoming calls, back button presses), it helps developers understand the Activity state transition mechanism. The article also covers the relationship between Activity states and process priority, and how to manage resources and save state data through lifecycle methods to ensure application stability and user experience across different scenarios.

Overview of Activity Lifecycle

The Android Activity lifecycle is a core concept in application development, defining the complete process from creation to destruction of an Activity. The system manages state transitions through a series of callback methods, and developers must understand the invocation timing and responsibilities of these methods to ensure correct application behavior in various scenarios.

Detailed Explanation of Core Lifecycle Methods

The Activity lifecycle comprises seven key methods, each invoked in specific states to handle initialization, resource management, or state preservation.

onCreate() Method

onCreate() is called when the Activity is first created, used for one-time initialization operations. Developers typically set up the user interface, bind data to lists, and initialize class-scope variables in this method. It receives a Bundle parameter to restore previously saved state, if any. Below is a typical onCreate() implementation example:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    if (savedInstanceState != null) {
        gameState = savedInstanceState.getString("gameState");
    }
    initializeViews();
}

After this method executes, the Activity enters the Started state, and the system sequentially calls onStart() and onResume().

onStart() Method

onStart() is called when the Activity becomes visible to the user, marking the preparation phase for the foreground. Developers can initialize UI-related components here, but note that this method executes quickly, and the Activity does not remain in this state for long. Example code:

@Override
public void onStart() {
    super.onStart();
    initializeUIComponents();
}

onResume() Method

onResume() is the starting point for the Activity entering the foreground and interacting with the user. In this state, the Activity is at the top of the stack and receives user input. Developers should resume resources released in onPause(), such as reconnecting to the network or starting sensors. The following code demonstrates camera component initialization:

@Override
public void onResume() {
    super.onResume();
    if (camera == null) {
        initializeCamera();
    }
}

When an interruptive event occurs (e.g., an incoming call), the system calls onPause(), and the Activity enters the Paused state.

onPause() Method

onPause() is the first indication that the Activity is losing focus, typically when another Activity covers it or the device goes to sleep. In this method, developers should pause operations that do not need to run in the background and release system resources (e.g., GPS or sensor handles). Note that this method should execute as quickly as possible to avoid delaying the start of new Activities. Example:

@Override
public void onPause() {
    super.onPause();
    if (camera != null) {
        camera.release();
        camera = null;
    }
}

onStop() Method

When the Activity becomes completely invisible, the system calls onStop(). Developers should release or adjust UI-related resources and perform CPU-intensive shutdown operations (e.g., saving data to a database). Compared to onPause(), this method is more suitable for resource management in multi-window mode. Code example:

@Override
public void onStop() {
    super.onStop();
    saveDataToDatabase();
    releaseUIResources();
}

onRestart() Method

onRestart() is called before the Activity restarts from the Stopped state, followed by onStart(). Developers can restore specific resources released in onStop() here. Example:

@Override
public void onRestart() {
    super.onRestart();
    reloadCachedData();
}

onDestroy() Method

onDestroy() is the final callback before the Activity is destroyed, which may occur due to user-initiated closure or system resource reclamation. Developers should clean up all unreleased resources here and use isFinishing() to distinguish the destruction reason. Code example:

@Override
public void onDestroy() {
    super.onDestroy();
    if (isFinishing()) {
        cleanUpPersistentResources();
    }
}

Lifecycle Invocation Sequences in Common Scenarios

Understanding the order of lifecycle methods in different user interactions is crucial. Below is an analysis of sequences in typical scenarios:

App First Launch

When the user first opens the app, the system calls methods in the order: onCreate()onStart()onResume(). This sequence completes the full initialization of the Activity and brings it to an interactive state.

Pressing the Home Button

When the user presses the Home button, the Activity moves to the background: onPause()onStop(). The Activity is invisible but may remain in memory.

Restoring from Recent Tasks

When the user reopens the app from the recent tasks list, the system calls: onRestart()onStart()onResume(). This sequence quickly restores the Activity to the foreground state.

Exiting via Back Button

When the user presses the Back button or calls finish(), the Activity is completely destroyed: onPause()onStop()onDestroy().

Incoming Call Interruption

When a call arrives, the current Activity pauses: onPause(). If the user answers the call, the Activity remains in the Paused state; if the call is declined, the system calls onResume() to resume interaction.

Activity States and Process Priority

The Android system manages process priority based on Activity state, affecting the likelihood of survival under low memory conditions:

Developers must properly save critical data in onStop() or onPause() to handle system reclamation scenarios.

Best Practices for State Saving and Restoration

To maintain consistent user experience, Activities need to save and restore state during configuration changes (e.g., screen rotation) or system reclamation. It is recommended to use onSaveInstanceState() combined with ViewModel to manage transient data:

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("userProgress", currentProgress);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
        currentProgress = savedInstanceState.getString("userProgress");
    }
}

This mechanism ensures that users can return to the previous state when they come back to the Activity.

Conclusion

Mastering the Android Activity lifecycle is fundamental to developing stable and efficient applications. By properly implementing the callback methods, developers can optimize resource usage, enhance user experience, and ensure correct application behavior across different scenarios. It is advisable to flexibly apply lifecycle management strategies based on specific requirements in practical development.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.