Activity Life Cycle in Android
Definition
In Android, an activity is a crucial component that represents a single screen with a user interface. Each activity goes through a series of stages during its life, known as the "activity life cycle." Understanding this cycle is essential for managing app behavior, resources, and interactions efficiently.
Life Cycle Methods
The activity life cycle consists of several callback methods, which are invoked by the Android system as the activity transitions through different states. These methods allow developers to manage the state of the activity appropriately, ensuring smooth performance and user experience. The primary callback methods are:
onCreate() – Called when the activity is first created.
onStart() – Called when the activity becomes visible to the user.
onResume() – Called when the user starts interacting with the activity.
onPause() – Called when the activity is partially obscured.
onStop() – Called when the activity is no longer visible to the user.
onRestart() – Called after the activity has been stopped, prior to restarting.
onDestroy() – Called before the activity is destroyed.
Detailed Explanation of Life Cycle Methods
a. onCreate()
Purpose:
Called when the activity is first created.
Actions:
Initialize essential components
set up UI using setContentView(),
restore saved states if any.
Example
Loading layout
initializing variables
setting up event listeners.
Format
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
b. onStart()
Purpose
Called when the activity becomes visible to the user.
Actions
Perform any actions needed to prepare the UI for interaction, such as refreshing data or starting animations.
Transition
The activity moves to the "started" state.
Format
@Override
protected void onStart() {
super.onStart();
// The activity is now visible
}
c. onResume()
Purpose
Called when the activity starts interacting with the user.
Actions
Resume any paused activities, such as restarting animations or refreshing user data.
Transition
The activity moves to the "resumed" state, where it is fully interactive.
Format
@Override
protected void onResume() {
super.onResume();
// The activity is now in the foreground and can interact with the user
}
d. onPause()
Purpose
Called when the system is about to put the activity into the background.
Actions
Pause ongoing tasks such as animations, media playback, or saving unsaved data.
Transition
The activity moves to the "paused" state but is still partially visible.
Format
@Override
protected void onPause() {
super.onPause();
// Pause tasks or animations when activity is partially obscured
}
e. onStop()
Purpose:
Called when the activity is no longer visible to the user.
Actions:
Stop heavy tasks or resources that are not needed, like network calls or releasing UI resources.
Transition:
The activity moves to the "stopped" state and may be killed by the system if memory is low.
Format
@Override
protected void onStop() {
super.onStop();
// Stop resources when the activity is not visible
}
f. onRestart()
Purpose:
Called when the activity is being restarted after being stopped.
Actions:
Perform any tasks to re-initialize the activity before it starts again, such as refreshing data or UI elements.
Transition:
The activity moves back to the "started" state.
Format
@Override
protected void onRestart() {
super.onRestart();
// The activity is being restarted
}
g. onDestroy()
Purpose:
Called before the activity is destroyed by the system, either because the user closed it or due to system conditions like low memory.
Actions:
Clean up resources, save final data, and release any system handles.
Transition:
The activity moves to the "destroyed" state, and the object is completely removed from memory.
Format
@Override
protected void onDestroy() {
super.onDestroy();
// Cleanup resources before activity is destroyed
}
Activity States and Transitions
The activity transitions through different states based on the methods mentioned:
Created: After onCreate() is called.
Started: After onStart() is called, and the activity is visible but not yet interactive.
Resumed: After onResume() is called, and the activity is in the foreground and interactive.
Paused: When the activity is partially visible (after onPause()).
Stopped: When the activity is completely hidden (after onStop()).
Destroyed: After onDestroy(), when the activity is removed from memory.
Example Scenario
Consider an app where the user fills out a form. During the life cycle:
The form UI is set up in onCreate().
When the user switches apps (onPause()), the form data is saved to avoid loss.
If the app is reopened (onResume()), the form restores the previous data.
Importance of Activity Life Cycle
Resource Management: Helps manage memory and system resources efficiently by releasing resources in onPause() or onStop().
State Handling: Ensures that user data is saved and restored appropriately across activity restarts.
Performance Optimization: Activities only consume resources when they are needed, improving the app’s overall performance.
User Experience: Ensures the app behaves predictably during transitions, such as orientation changes or multitasking.