The Activity Lifecycle in Android defines the states and transitions an Activity goes through during its existence. Understanding the lifecycle is crucial for managing resources efficiently, handling state changes, and ensuring smooth user experiences.
Lifecycle Methods
Here are the key lifecycle methods in the order they are typically called:
1. onCreate()
- Called when the Activity is created.
- Initialize UI components, set up data bindings, or restore saved states.
- Triggered once during the lifetime of an Activity.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Initialize components here
}
2. onStart()
- Called when the Activity becomes visible to the user.
- The Activity is not yet interactive but visible on the screen.
override fun onStart() {
super.onStart()
// Perform actions like refreshing UI or data
}
3. onResume()
- Called when the Activity starts interacting with the user.
- The Activity enters the foreground state.
- Ideal for starting animations, opening cameras, or other active tasks.
override fun onResume() {
super.onResume()
// Resume tasks, like restarting animations or playback
}
4. onPause()
- Called when the Activity is partially obscured (e.g., a dialog appears).
- Used to save data or release resources like animations, sensors, or music.
override fun onPause() {
super.onPause()
// Pause animations or release resources
}
5. onStop()
- Called when the Activity is no longer visible to the user.
- Free up resources and save persistent data.
- It’s always followed by either onRestart() or onDestroy().
override fun onStop() {
super.onStop()
// Save application data or release resources
}
6. onRestart()
- Called when the Activity is visible again after being stopped.
- Often used to reinitialize resources or refresh data.
override fun onRestart() {
super.onRestart()
// Reinitialize components or update UI
}
7. onDestroy()
- Called when the Activity is destroyed or the app process is terminated.
- Cleanup resources like threads, databases, or listeners.
override fun onDestroy() {
super.onDestroy()
// Perform final cleanup
}
Diagram of Activity Lifecycle
Here’s a simplified representation of the lifecycle:
- Launch:
onCreate
→onStart
→onResume
- Foreground (Interactive): Active in
onResume
- Background or Paused:
onPause
→onStop
- Restart (if needed):
onRestart
→onStart
→onResume
- Destroy:
onStop
→onDestroy
Lifecycle Scenarios
App in Foreground
onCreate
→onStart
→onResume
App Goes to Background
onPause
→onStop
App Returns to Foreground
onRestart
→onStart
→onResume
App is Closed
onPause
→onStop
→onDestroy
Tips for Lifecycle Management
- Save Instance State: Use
onSaveInstanceState
to store data in case the Activity is recreated. - Avoid Memory Leaks: Release heavy resources (e.g., listeners, sensors) in
onPause
oronStop
. - Optimize Performance: Only load UI-heavy resources in
onResume
.
Would you like a Kotlin code example demonstrating these lifecycle methods? 🚀