Activity Lifecycle

The Activity Lifecycle: onCreate to onDestroy

An activity moves through the states drawn below, and the system calls one method on each move. onCreate runs once per instance: set up the UI there. onStart and onStop bracket the time the activity is visible, onResume and onPause the time it has focus. onDestroy is the last call, but when the system kills the process to reclaim memory no callback runs at all, so work that must persist belongs in onStop or earlier.

Activity states, the callbacks between them, and the path through process death
Activity states, the callbacks between them, and the path through process death

MainActivity logs each callback, with the tag BookNestLife, so logcat shows the real order:

MainActivity.kt: logging every lifecycle callback (imports omitted)Kotlin
private const val TAG = "BookNestLife"
class MainActivity : ComponentActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    Log.d(TAG, "onCreate restored=${savedInstanceState != null}")
    enableEdgeToEdge()                          // draw behind the status and navigation bars
    setContent { MaterialTheme { BookDetail() } }
  }
  override fun onStart() { super.onStart(); Log.d(TAG, "onStart") }
  override fun onResume() { super.onResume(); Log.d(TAG, "onResume") }
  override fun onPause() { Log.d(TAG, "onPause"); super.onPause() }
  override fun onStop() { Log.d(TAG, "onStop"); super.onStop() }
  override fun onSaveInstanceState(out: Bundle) {
    super.onSaveInstanceState(out); Log.d(TAG, "onSaveInstanceState")
  }
  override fun onDestroy() {
    Log.d(TAG, "onDestroy changing=$isChangingConfigurations"); super.onDestroy()
  }
}

Launched from the home screen, with adb logcat -v time -s BookNestLife running, the first three steps appear:

Output of 65
09-24 18:36:12.652 D/BookNestLife( 9238): onCreate restored=false
09-24 18:36:12.730 D/BookNestLife( 9238): onStart
09-24 18:36:12.751 D/BookNestLife( 9238): onResume