Android activity lifecycle is a crucial concept for building responsive and efficient applications. By understanding the lifecycle, developers can optimize resource usage, manage transitions between activities, and handle user interactions smoothly. This article provides a comprehensive overview of different scenarios in the activity lifecycle, illustrated with clear pictorial representations.
Activity Lifecycle Overview
An Android activity goes through a series of lifecycle states:
onCreate(): Activity is being created.
onStart(): Activity becomes visible.
onResume(): Activity starts interacting with the user.
onPause(): Activity is partially obscured.
onStop(): Activity is completely hidden.
onDestroy(): Activity is being destroyed.
onRestart(): Activity is being restarted after being stopped.
Here’s a visual representation of the lifecycle:
onCreate()
↳
onStart()
↳
onResume()
↴
onPause()
↴
onStop()
↴
onDestroy()
↳
onRestart()
Scenarios and Lifecycle Callbacks
1. Transition from Activity A to Activity B
When navigating from Activity A to Activity B:
Activity A:
onPause(): Called when Activity A is partially obscured.
onStop(): Called when Activity A is completely hidden.
Activity B:
onCreate(): Called when Activity B is first created.
onStart(): Called when Activity B becomes visible.
onResume(): Called when Activity B starts interacting with the user.
Pictorial Representation:
Activity A: onPause() ➔ onStop() Activity B: onCreate() ➔ onStart() ➔ onResume()
2. Returning from Activity B to Activity A
When navigating back from Activity B to Activity A:
Activity B:
onPause(): Called when Activity B is partially obscured.
onStop(): Called when Activity B is completely hidden.
onDestroy(): Called before Activity B is destroyed.
Activity A:
onRestart(): Called if Activity A was stopped.
onStart(): Called when Activity A becomes visible again.
onResume(): Called when Activity A starts interacting with the user again.
Pictorial Representation:
Activity B:
onPause() ➔ onStop() ➔ onDestroy()
Activity A:
onRestart() ➔ onStart() ➔ onResume()
3. Orientation Change
When the device orientation changes, the activity is destroyed and recreated:
Activity A:
onPause()
onStop()
onDestroy()
onCreate()
onStart()
onResume()
Pictorial Representation:
Activity A:
onPause() ➔ onStop() ➔ onDestroy()
onCreate() ➔ onStart() ➔ onResume()
4. Pressing Home Button
When the user presses the home button:
Activity A:
onPause(): Called when the activity is partially obscured.
onStop(): Called when the activity is completely hidden.
Pictorial Representation:
Activity A:
onPause() ➔ onStop()
5. Returning to Activity from Home Screen
When the user returns to the app from the home screen:
Activity A:
onRestart(): Called if the activity was stopped.
onStart(): Called when the activity becomes visible.
onResume(): Called when the activity starts interacting with the user again.
Pictorial Representation:
Activity A:
onRestart() ➔ onStart() ➔ onResume()
6. Receiving a Phone Call
When a phone call interrupts the activity:
Activity A:
onPause(): Called when the activity is partially obscured.
onStop(): Called if the phone call screen fully covers the activity.
Pictorial Representation:
Activity A:
onPause() ➔ onStop()
7. Ending a Phone Call
When the user returns to the activity after the call:
Activity A:
onRestart()
onStart()
onResume()
Pictorial Representation:
Activity A:
onRestart() ➔ onStart() ➔ onResume()
8. Configuration Changes
When configuration changes occur (e.g., language or font size):
Activity A:
onPause()
onStop()
onDestroy()
onCreate()
onStart()
onResume()
Pictorial Representation:
Activity A:
onPause() ➔ onStop() ➔ onDestroy()
onCreate() ➔ onStart() ➔ onResume()
Best Practices for Handling Lifecycle
Save State: Use
onSaveInstanceState()
to save the activity state during configuration changes or transitions.Release Resources: Release resources (e.g., database connections, listeners) in
onPause()
oronStop()
to prevent memory leaks.Manage Background Work: Use
ViewModel
andLiveData
to retain data across configuration changes without restarting tasks.Avoid Long Operations in Callbacks: Do not perform long-running operations in lifecycle callbacks like
onCreate()
oronResume()
.Test Different Scenarios: Simulate transitions (e.g., orientation changes, interruptions) to ensure your app handles them gracefully.
By understanding and leveraging the Android activity lifecycle, you can build robust and user-friendly applications that handle various scenarios seamlessly. Proper lifecycle management improves the user experience and ensures efficient use of system resources.
Happy Coding :)