The idea: the system calls your activity
An Android screen is an activity. Your code never decides when it starts or stops. The system does, and it tells the activity by calling its lifecycle callbacks. There are six core ones: onCreate(), onStart() and onResume() on the way in, onPause(), onStop() and onDestroy() on the way out. onRestart() comes before onStart() when a stopped activity comes back.
The canvas has four parts. The phone shows the screen and three tap counters. The diagram lights the callback that is running. system_server (the ActivityTaskManager) keeps the task: a back stack of activity records, each with its state and its last saved Bundle. The app process holds the real activity objects and the ViewModelStore. Each callback flies from the record to the object.
Visible, in front, stopped
| After | State | What it means |
|---|---|---|
onCreate() | created | The object exists. Set the layout, make the ViewModel, read the saved Bundle. |
onStart() | started | Visible, but not yet taking input. |
onResume() | resumed | In front, has the focus, gets touches. |
onPause() | paused | Lost the focus. Maybe still partly visible. Keep it short. |
onStop() | stopped | Not visible. The object is still in memory. |
onDestroy() | destroyed | Finished, or about to be made again after a configuration change. |
A cold start is onCreate(null), onStart(), onResume(). The null means there is no saved state yet.
Home and an incoming call
Home and a full-screen call do the same thing to the app. Another screen covers it, so it is stopped: onPause(), onStop(), onSaveInstanceState(). On Android 9 (API 28) and later, onSaveInstanceState() runs after onStop(); before that it ran before onStop(). The object is not destroyed. When the user comes back, the same object gets onRestart(), onStart(), onResume(), and every counter is still there (Demo 1: launch, Home, come back; Demo 3: an incoming call).
Back on the root activity of the launcher's task is the same as Home since Android 12: the task moves to the background and the activity is not finished. A screen that only partly covers yours, such as a permission dialog, only pauses it: onPause(), then onResume() when it goes away. An AlertDialog shown by your own activity calls no callback at all.
Rotation: destroyed and made again
Turning the device changes the configuration (orientation, screen size). By default the system destroys the activity object and makes a new one, so the new layout can load. The order is onPause, onStop, onSaveInstanceState, onDestroy on the old object, then onCreate(Bundle), onStart, onRestoreInstanceState, onResume on the new one (Demo 2: rotation loses the field).
The page counts taps three ways. After three taps and one rotation:
- A plain field (
private int taps) is 0. It lived in the old object. - The ViewModel (
MainViewModel.taps) is still 3. InonDestroy(),isChangingConfigurations()is true, so the ViewModelStore is handed to the new object, andViewModelProviderreturns the same ViewModel. - The saved state (
savedTaps) is 3. It was written withoutState.putInt("taps", 3)and read back inonCreate(savedInstanceState).
The back stack
A task is the stack of activities the user moved through. startActivity() pushes a new one on top; Back pops the top one and finishes it. The two activities' callbacks interleave in a fixed order: the new one cannot start until the old one has paused, and the old one stops only once the new one covers it (Demo 4: back stack).
Keep onPause() short. The next activity waits for it before it can start.
Process death
A stopped app can be killed at any time to free memory. No callback runs, not even onDestroy(). The objects, their fields and the ViewModel die with the process. The records and Bundles live in system_server, so they survive. When the user comes back, the system starts a new process (pid 4121 → 4387) and makes the top activity again with onCreate(Bundle). After two taps: field 0, ViewModel 0, saved 2 (Demo 5: process death).
Only the top activity is made again. An activity below it stays a record with a Bundle until Back reaches it; then it gets its own onCreate(Bundle) (Demo 6: process death with two activities). To the user it looks as if the app never stopped, if the app saved its state.
What survives what
| Where the counter lives | Home, call | Rotation | Process death | Back (finish) |
|---|---|---|---|---|
| field in the activity | ✓ kept | ✗ lost | ✗ lost | ✗ lost |
| ViewModel | ✓ kept | ✓ kept | ✗ lost | ✗ cleared |
| saved instance state (Bundle) | ✓ kept | ✓ kept | ✓ kept | ✗ thrown away |
The Bundle is small and goes through a system call, so keep it to IDs and a few values. Anything bigger belongs in a database, with the ID in the Bundle.
See also A Linux Process from fork() to Reaping for the process underneath an app, The State Pattern and Finite State Machines for state machines in code and hardware, and MVC vs MVVM for where a ViewModel sits in an app.
What the page leaves out
Fragments have their own, longer lifecycle inside the activity's. Jetpack LifecycleObserver and repeatOnLifecycle let other classes react to these callbacks; SavedStateHandle lets a ViewModel itself survive process death. Views with an ID save their own state (text, scroll position) in the Bundle. An app can opt out of rotation restarts with android:configChanges and get onConfigurationChanged() instead. Multi-window mode, picture-in-picture, launch modes (singleTop, singleTask), intent flags and swiping the app away from Recents are left out; the page also rotates only while MainActivity is in front.