Interview frequency: …
Short answer
The system recreates the Activity entirely; we preserve non-transient data through a ViewModel, and essential lightweight state (such as identifiers and input text) through SavedStateHandle or onSaveInstanceState so it survives process death.
Explanation
During a language change or screen rotation, the new resources are applied by rebuilding the Activity (onDestroy and then onCreate are called). The ViewModel retains heavy data and screen states as long as the Activity is not deliberately closed, while onSaveInstanceState or SavedStateHandle is used to save text data and lightweight item identifiers sufficient to rebuild the screen after process death.
Code example
class DetailViewModel(
private val savedStateHandle: SavedStateHandle
) : ViewModel() {
// Restores the ID even after the system kills the app
val itemId: String = checkNotNull(savedStateHandle["item_id"])
}Common mistakes
- Trying to store large objects or a Bitmap in the onSaveInstanceState bundle, causing a TransactionTooLargeException.
- Using android:configChanges in the manifest to escape handling the screen rebuild properly.