How it works
The explanation, with examples you can run.
The states
class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { // created: set up the UI once super.onCreate(savedInstanceState) setContent { App() } } override fun onStart() { super.onStart() } // visible override fun onResume() { super.onResume() } // in front, receiving input override fun onPause() { super.onPause() } // losing focus (dialog, multi-window) override fun onStop() { super.onStop() } // no longer visible override fun onDestroy() { super.onDestroy() } // finishing, or being recreated}Two rules of thumb. First, pair things at the same level: what you start in onStart you stop in onStop. Second, don't count on onDestroy: when the process is killed in the background, it never runs. After onStop, assume you may not get another callback.
You rarely override these by hand any more. Lifecycle-aware components observe them for you (LifecycleObserver, repeatOnLifecycle, collectAsStateWithLifecycle), and in Compose most per-screen setup lives in effects.
Configuration changes
The Configuration object holds things resources depend on: orientation, screen size, locale, font scale, dark mode, keyboard. When one changes, the default is to destroy and recreate the activity so the right layouts, strings and dimensions are loaded. On foldables, tablets and desktop windows this happens far more often than on a phone held upright.
What survives the recreation:
| Where | Rotation | Process death |
|---|---|---|
Fields in the activity, plain remember | lost | lost |
ViewModel | kept | lost |
rememberSaveable, SavedStateHandle | kept | kept |
| Room, DataStore, files | kept | kept |
You can opt out with android:configChanges in the manifest, and then receive onConfigurationChanged() instead. That is reasonable for a Compose-only activity, where reading LocalConfiguration.current recomposes the affected UI, but you take over responsibility for everything that reads resources, including embedded Views.
Version note: starting with Android 17 (API 37) the system no longer recreates activities by default for a handful of changes that rarely need new resources (keyboard, navigation, touchscreen, color mode, and desk UI mode). Apps that relied on a restart for those can opt back in with android:recreateOnConfigChanges. Rotation, size and locale still recreate.
Don't confuse this with process death
A rotation keeps the process; process death doesn't. A screen can pass every rotation test and still lose the user's input after a few minutes in the background. Test both.
Check yourself
3 questions. Get them all right to finish this topic.
0 of 3 right so far. Get all three to finish this topic.