The Android activity lifecycle is the set of callbacks the system calls as an Activity is created, shown, hidden and destroyed: onCreate(), onStart(), onResume(), onPause(), onStop(), onRestart() and onDestroy(). This tutorial shows each callback with a logging example in Kotlin and Java, explains when it runs and what you should do in it.
Understanding the lifecycle prevents leaks, lost user input and crashes after rotation or process death. It is also the base for newer tools such as ViewModel and lifecycle-aware components.
| Callback | When it is called | Typical use |
|---|---|---|
onCreate() | The activity is first created | Inflate the layout, set up the UI, restore saved state |
onStart() | The activity becomes visible | Start things that need visibility |
onResume() | The activity is in the foreground and interactive | Start camera, sensors, animations |
onPause() | The user is leaving or another window covers part of it | Pause work, save light state, release exclusive resources |
onStop() | The activity is no longer visible | Release heavy resources, save data |
onRestart() | A stopped activity is about to start again | Rarely needed; work in onStart() instead |
onDestroy() | The activity is finishing or being recreated | Final cleanup |
The activity has three nested phases. The entire lifetime runs from onCreate() to onDestroy(). The visible lifetime runs from onStart() to onStop(). The foreground lifetime runs from onResume() to onPause().
Add one log line per callback, run the app and watch Logcat. Kotlin is the recommended language for Android, and Java works identically.
The Java version of the same activity:
Always call the super method first, as shown, otherwise Android throws an exception.
Launching the app, pressing Home, and opening the app again produces this order:
Pressing Back finishes the activity and ends with onPause, onStop, onDestroy. Rotating the device destroys and recreates the activity by default, so you see onDestroy followed by onCreate again with a non-null savedInstanceState.
Do not keep the only copy of user data in fields, because the system can destroy the activity at any time. Use these tools:
onSaveInstanceState(Bundle) for small UI state that must survive recreation and process death.ViewModel for UI data that must survive configuration changes such as rotation.rememberSaveable in Jetpack Compose, or SavedStateHandle in a ViewModel.DataStore for data that must persist for good.Instead of adding logic to every callback, observe the lifecycle from a component.
In coroutines code, use repeatOnLifecycle(Lifecycle.State.STARTED) to collect flows only while the UI is visible.
onPause runs when the activity loses focus but may still be partly visible, for example behind a dialog or in multi-window mode. onStop runs when it is completely hidden.
No. If the system kills the process to free memory, onDestroy may never run. Save important data earlier, in onPause or onStop.
Start in onResume and release in onPause or onStop, depending on the resource and on multi-window support.
The activity still has the same callbacks. Compose adds its own effects such as DisposableEffect, which tie work to composition.
An activity moves through onCreate, onStart, onResume, onPause, onStop, and onDestroy, with onRestart when it returns from the stopped state. Put setup and teardown in matching pairs, save state with ViewModel and saved state APIs, and use lifecycle observers to keep your logic out of the activity.