In an Android Activity, which lifecycle callbacks run, and in what order, when the user rotates the device, presses Home, and presses Back?
answer
- six callbacks, two directions
- rotation means a new instance
- Home behaves like full cover
- root launcher activity since Android 12
basics
~20 sRotation destroys and recreates an Activity: onPause, onStop, onDestroy, then onCreate, onStart, onResume on a new instance. Home runs onPause and onStop and keeps it. Back adds onDestroy, except on a root launcher activity on Android 12+.
solid answer
~40 sBy default rotation is a configuration change, so the running instance gets `onPause`, `onStop` and `onDestroy`, and a **new** instance gets `onCreate` (with the saved `Bundle`), `onStart` and `onResume`; on API 28 and higher `onSaveInstanceState` runs after `onStop`. Pressing Home hides the activity completely, so it gets only `onPause` and `onStop`, and coming back runs `onRestart`, `onStart` and `onResume` on the same instance. Back on an ordinary activity runs `onPause`, `onStop` and `onDestroy`, and that instance is gone for good with no saved state. On Android 12 and later, Back on the root launcher activity moves the task to the background instead, like Home. Android 17 (API 37) stopped recreating activities for keyboard, navigation, touchscreen, color-mode and desk-mode changes, but rotation still recreates them.
go deeper
Recite the six callbacks in order and say which run on rotation, Home and Back. Knowing that rotation produces a brand-new instance is the point most screeners check.
Explain why fields vanish on rotation, where onSaveInstanceState sits relative to onStop since API 28, and the overlapping order when one activity starts another.
Show you place work by lifecycle pairs, never trust onDestroy for persistence, and know the Android 12 root-activity Back change and Android 17's narrower restart list.
Frame lifecycle handling as a team rule: lifecycle-aware components instead of logic in callbacks, and recreation and process death tested on every screen rather than assumed rare.
## The six callbacks and the states they mark An Android `Activity` moves through a small set of states, and the system calls one callback as it enters each one. The **core six** are: - `onCreate(savedInstanceState)` - the activity is **Created**. Do one-time setup here. The `Bundle` is `null` the first time and carries the saved instance state when the activity is being recreated. - `onStart()` - the activity is **Started** and becomes visible. - `onResume()` - the activity is **Resumed**: in the foreground and interactive. - `onPause()` - the first sign that the user is leaving. The activity is no longer in the foreground but may still be visible, for example in multi-window mode. - `onStop()` - the activity is no longer visible. - `onDestroy()` - the activity is finishing, or the system is destroying it temporarily for a configuration change. A seventh, `onRestart()`, runs when a **stopped** instance comes back, just before `onStart()`. The key interview point is that the callbacks come in mirrored pairs: what you acquire in `onStart` you release in `onStop`, and what you acquire in `onResume` you release in `onPause`. ## Rotation creates a new instance Rotation is a **configuration change**, and the platform's default answer to a configuration change is **activity recreation**. The sequence is: 1. The current instance gets `onPause`, then `onStop`. 2. On API 28 and higher the system then calls `onSaveInstanceState`; before API 28 that call came before `onStop`. 3. The current instance gets `onDestroy`. Calling `isFinishing()` here returns `false`, because the activity is coming back. 4. The system immediately creates a **new** instance in the new configuration and calls `onCreate` with the saved `Bundle`, then `onStart` and `onResume`. Because the old object is thrown away, any state kept only in its fields is lost. That is why rotation is the cheapest way to expose a state-handling bug. Android 17 (API level 37) narrowed which changes restart an activity by default. The system no longer recreates the activity for `CONFIG_KEYBOARD`, `CONFIG_KEYBOARD_HIDDEN`, `CONFIG_NAVIGATION`, `CONFIG_TOUCHSCREEN`, `CONFIG_COLOR_MODE`, or a `CONFIG_UI_MODE` change into or out of desk mode. Those arrive in `onConfigurationChanged()` instead, and an app that needs the old restart opts back in with the `android:recreateOnConfigChanges` manifest attribute. Orientation and screen size are not on that list, so rotation still recreates the activity unless it declares those changes in `android:configChanges`. ## Home, Recents and covering activities - **Home or Overview**: the system treats the activity as completely covered. It gets `onPause` and `onStop`, the object stays in memory, and its task goes to the background. Returning runs `onRestart`, `onStart` and `onResume` on the **same** instance. - **A dialog-style activity that only partly covers it**: the covered activity gets `onPause` only, and `onResume` when it regains focus. - **Activity A starts Activity B** in the same app: A's `onPause` runs, then B's `onCreate`, `onStart` and `onResume`, and only then A's `onStop` if A is no longer visible. The two transitions overlap, so B must not assume A has already saved in `onStop`. ## Back For an ordinary activity, Back runs `onPause`, `onStop` and `onDestroy`; the activity is popped from the back stack and `isFinishing()` is `true`. The user dismissed it, so the system keeps **no** instance state for it. The exception is the **root launcher activity**, the one whose intent filter has `ACTION_MAIN` and `CATEGORY_LAUNCHER`. On Android 11 and lower Back finishes it. On **Android 12 and higher** the system moves the activity and its task to the background instead, exactly as Home does, so it gets `onPause` and `onStop` but not `onDestroy`, and a later launch is a warm start. Custom back handling should use the AndroidX Activity back APIs rather than overriding `onBackPressed()`, so this system default still applies. ## Summary | Event | Callbacks on the current instance | What happens next | |---|---|---| | Rotation | `onPause`, `onStop`, `onDestroy` | a new instance runs `onCreate`, `onStart`, `onResume` | | Home or Recents | `onPause`, `onStop` | returning runs `onRestart`, `onStart`, `onResume` | | Back, ordinary activity | `onPause`, `onStop`, `onDestroy` | the previous activity resumes | | Back, root launcher activity, Android 12+ | `onPause`, `onStop` | the task waits in the background | | Partly covering activity | `onPause` | `onResume` when it closes | ## Where work belongs - Keep `onPause` short: it is not the place for database writes or network calls. - Do heavier saving in `onStop`, because a stopped activity's process is a likely target when the system needs memory. - Never rely on `onDestroy` for saving: when the system kills a background process, there is **no guarantee** `onDestroy` runs at all.
- How can onDestroy tell a rotation apart from the user leaving the activity for good?Check `isFinishing()`. It returns `true` when the activity is finishing because the user pressed Back or code called `finish()`, and `false` when the system is destroying it to recreate it for a configuration change such as rotation. Better still, keep data that must outlive rotation outside the activity so `onDestroy` does not need to decide.
- In a photo editor, where should the camera preview start and stop, and where should the draft be saved?Start the preview on resume and release it on pause if it should run only while the editor has focus, or pair start and stop instead if it should keep running while visible in multi-window. Do not save the draft in `onPause`, which is brief and may not finish disk or network work; save it in `onStop` or, better, when the user makes the edit.
- What did Android 17 change about configuration changes, and how does an app get the old restart back?From Android 17 (API 37) the system no longer restarts an activity by default for keyboard, keyboard-hidden, navigation, touchscreen and color-mode changes, or for entering or leaving desk UI mode. The activity stays alive and receives `onConfigurationChanged()`. An app that reloads resources by restarting lists those changes in the new `android:recreateOnConfigChanges` manifest attribute.
- Rotation only calls onPause and onResume on the same instance.
- onDestroy always runs before the system kills a background process.
- Pressing Home destroys the activity, so onDestroy is where to save.
- Back always finishes the activity, even the root launcher one on Android 12+.
- onPause is the right place for database writes and network calls.
- When A starts B, A is fully stopped before B is created.
Assumes Android 17 (API 37): it stopped recreating activities for keyboard, navigation, touchscreen, color-mode and desk-mode changes, while rotation still recreates; Back on a root launcher activity backgrounds the task since Android 12.