Config Changes & Process Death Interview Questions
ANDROID › Lifecycle
What happens to an Activity on a configuration change, and how do you keep UI state across it?
What a strong answer covers: By default the activity is destroyed and recreated with the new configuration so resources reload. State survives through a ViewModel (kept in the ViewModelStore across recreation), onSaveInstanceState or rememberSaveable for small UI state, and persistent storage for data. Views with ids also save some of their own state automatically.
What is process death, and how is it different from a configuration change?
What a strong answer covers: When the app is in the background, the system can kill the process to reclaim memory. When the user returns, Android recreates the activity stack with saved Bundles, but all in-memory objects, ViewModels and singletons are gone. ViewModels survive configuration changes but not process death; only saved instance state, SavedStateHandle and persistent storage do.
What is SavedStateHandle, and when would you use it instead of plain ViewModel properties?
What a strong answer covers: SavedStateHandle is a key-value map passed to the ViewModel that's backed by the saved instance state Bundle, so its values survive process death as well as configuration changes. It also holds navigation arguments. Use it for small state the user would expect to come back, like a search query or selected item id, typically with getStateFlow(key, default). Keep it small, since it's still a Bundle.
When is it reasonable to handle configuration changes yourself with android:configChanges, and what are the risks?
What a strong answer covers: It's reasonable for some cases like video players or games that would be disruptive to restart, or for Compose-only apps that handle changes in composition. The risk is that resources don't reload automatically: you have to respond in onConfigurationChanged, and layouts, strings or dimensions for the new configuration may be wrong. It doesn't help with process death at all, so it isn't a fix for state loss.
A user fills in a multi-step form, switches apps for 20 minutes, and comes back to an empty form. Explain what probably happened and how you'd fix it end to end.
What a strong answer covers: The process was likely killed in the background. The activity was restored, but the form was held only in ViewModel fields or a singleton, which were lost. Fixes: keep form inputs in SavedStateHandle (or rememberSaveable for simple screens), persist drafts to DataStore or Room for longer forms, restore the current step from saved state, and reproduce with adb shell am kill in testing. A strong answer also checks the navigation back stack restores to the same step.