Android Activity Lifecycle: A Complete Guide
When you build Android apps, Activity is the core component that represents a screen with a user interface. Every Activity goes through different lifecycle states, and understanding them is crucial for building stable and efficient apps.
This guide will explain:
- The different lifecycle methods.
- Scenarios for each method.
- How to handle state with
savedInstanceState.
1. What is Activity Lifecycle?
Activity lifecycle defines how an activity behaves as it is created, started, resumed, paused, stopped, or destroyed. Android calls specific lifecycle methods when the activity transitions between states. Knowing these methods helps handle:
- Saving data before the activity is destroyed.
- Releasing resources when activity is not visible.
- Initializing UI components at the right time.
2. onCreate() and savedInstanceState
What is onCreate()?
onCreate() is the first method called when an activity is created. Think of it as the setup phase of your screen:
- Define the layout of your activity (
setContentView). - Initialize UI components like buttons or text views.
- Bind data or start essential services.
It’s called once when the activity starts, or again if the activity is destroyed and recreated.
What is savedInstanceState?
savedInstanceState is a Bundle that stores the activity’s temporary state. It is useful in scenarios like:
- Device rotation
- Activity killed by system in background
- Returning to an activity after a configuration change
If the activity is recreated, savedInstanceState contains data you saved earlier in onSaveInstanceState. If it’s the first time the activity is created, savedInstanceStateis null.
Example:
class MainActivity : AppCompatActivity() {
private var counter = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Restore saved state if activity is recreated
if (savedInstanceState != null) {
counter = savedInstanceState.getInt("COUNTER_KEY", 0)
}
val textView = findViewById<TextView>(R.id.textView)
textView.text = "Counter: $counter"
val button = findViewById<Button>(R.id.button)
button.setOnClickListener {
counter++
textView.text = "Counter: $counter"
}
}
// Save data before activity might be destroyed
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt("COUNTER_KEY", counter)
}
}Explanation:
onCreate()sets up the UI and restores state if available.onSaveInstanceState()is called before destruction to save transient data.- This ensures that
countervalue survives rotation or recreation.
3. Other Lifecycle Methods and Scenarios
Opening an Activity
override fun onStart() {
super.onStart()
Log.d("Lifecycle", "onStart called")
}
override fun onResume() {
super.onResume()
Log.d("Lifecycle", "onResume called")
}Flow: onCreate -> onStart -> onResume
Use this to initialize UI and resources when activity opens.
Navigating to Another Activity
override fun onPause() {
super.onPause()
Log.d("Lifecycle", "onPause called")
}
override fun onStop() {
super.onStop()
Log.d("Lifecycle", "onStop called")
}Flow: onPause -> onStop
Triggered when activity is partially or fully hidden.
Returning to a Stopped Activity
override fun onRestart() {
super.onRestart()
Log.d("Lifecycle", "onRestart called")
}Flow: onRestart -> onStart -> onResume
Use onRestart to refresh data if the activity was stopped.
Closing an Activity
override fun onDestroy() {
super.onDestroy()
Log.d("Lifecycle", "onDestroy called")
}Flow: onPause -> onStop -> onDestroy
Occurs when the activity is finished or killed.
Device Rotation
When the device rotates, the activity is destroyed and recreated:
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putString("KEY", "Hello")
}
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
val value = savedInstanceState.getString("KEY")
}Flow: onPause -> onStop -> onDestroy -> onCreate(with bundle value if it was saved in onSaveInstanceState) -> onStart -> onResume
Use this to preserve data and restore UI state.
Get Manish Kumar’s stories in your inbox
Join Medium for free to get updates from this writer.
onRestoreInstanceState is called after onStart(), but only if savedInstanceState is not null. Its main purpose:
- Cleanly separate restoring state from initialization in
onCreate(). - Useful if you want to restore UI state after the activity has been fully started.
while onSaveInstanceState() is called after onPause() only when Android things this Activity is going to be recreated and if we call finish() we are closing activity intentionally then this method(onSaveInstanceState) is not triggered.
Activity with Dialog or Transparent Activity
- Underlying activity:
onPause() - New dialog-style activity:
onCreate -> onStart -> onResume
The underlying activity is paused but not stopped.
4. Tips for Handling Lifecycle
- Use
onResumefor resources needed during foreground only (camera, GPS). - Use
onPauseto save user state and release resources. - Avoid heavy operations in
onStartoronResume; use background threads. onDestroymay not always be called; don’t rely solely on it for critical data.- Handle configuration changes to prevent unnecessary recreation.
5. Quick Reference (Lifecycle Flow)
- Start Activity:
onCreate -> onStart -> onResume - Another Activity appears:
onPause -> onStop - Return to Activity:
onRestart -> onStart -> onResume - Finish Activity:
onPause -> onStop -> onDestroy - Rotate Device:
onPause -> onStop -> onDestroy -> onCreate -> onStart -> onResume
Hope this makes you comfortable with android development. Happy coding!!







