Android path · The process · Topic 2 of 12

The activity lifecycle and configuration changes

Not started

An activity is one screen's entry point, and the system moves it through a fixed set of states as the user arrives, leaves and comes back. You don't call these callbacks; you react to them. The most surprising part for newcomers is configuration changes: rotate the screen and, by default, the activity you were in is thrown away and a new one is built.

Why it matters: Rotating the phone, changing the language or resizing a window destroys and recreates the activity by default. Anything held only in the activity is lost each time.

  1. 1Six callbacks, three states that matter

    Created, started (visible) and resumed (in focus). Pair setup and teardown at the same level: start in onStart, stop in onStop.

  2. 2A config change is a new activity

    By default the old instance is destroyed and a new one is created with the new resources. Your process stays.

  3. 3Three places for state

    ViewModel survives the recreation, saved state survives process death too, and disk survives everything.

1

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:

WhereRotationProcess death
Fields in the activity, plain rememberlostlost
ViewModelkeptlost
rememberSaveable, SavedStateHandlekeptkept
Room, DataStore, fileskeptkept

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.

2

Check yourself

3 questions. Get them all right to finish this topic.

  1. 1.The user rotates the phone. With default settings, what happens to the activity?
  2. 2.Where should you start and stop listening for location updates that only matter while the screen is visible?
  3. 3.Your app declares android:configChanges="orientation|screenSize". What do you now have to do?

0 of 3 right so far. Get all three to finish this topic.