Android development / Editorial solution guide
Repair state loss after rotation or process recreation
Identify the state owner and restoration scenario before choosing rememberSaveable, ViewModel, saved state, or durable storage.
Read-only knowledge base ยท No public comments
Recognize the symptoms
Text resets after rotation, selection disappears on return, or a list reloads with the wrong record after the system recreates the process. A draft may survive rotation yet disappear later, indicating that configuration retention worked while process restoration did not. Capture the exact journey, including Back, task dismissal, force-stop, navigation away, and whether the app was backgrounded before the failure.
Understand the likely causes
- An ordinary local variable is recreated; remember lasts only while its composition owner exists; a ViewModel survives appropriate configuration changes but not process death. Saved state is tied to supported task restoration and is not permanent storage. Duplicate state copies and manually recreated ViewModels can also reset values even when the chosen API would otherwise be suitable.
Classify the missing value and lifetime
Write down each value: selected record id, draft text, scroll position, confirmed record, or pending upload. Decide which should survive leaving the composition, configuration recreation, system process death, and a deliberate new session. Small transient UI values may use saveable state; screen business state belongs in an owner-managed ViewModel; important records and valuable drafts belong in durable storage. This classification prevents solving every symptom by moving all data into a singleton, which creates leaks and still disappears with the process.
Keep one owner and save small inputs
For a search query owned by a ViewModel, SavedStateHandle can preserve the small reconstruction input through supported system restoration. Obtain the ViewModel through a saved-state-aware factory or DI integration attached to the correct navigation or activity owner. Do not construct it directly inside a composable. The example below preserves a query; the resulting list should be reconstructed from a repository. Large datasets and images should not be placed in the handle merely because they are easy to serialize.
class QueryViewModel(private val saved: SavedStateHandle) : ViewModel() {
val query: StateFlow<String> = saved.getStateFlow("query", "")
fun editQuery(value: String) { saved["query"] = value }
}
@Composable
fun QueryRoute(viewModel: QueryViewModel) {
val query by viewModel.query.collectAsStateWithLifecycle()
TitleField(title = query, onTitleChange = viewModel::editQuery)
}
Use lifecycle saved-state, lifecycle-runtime-compose, StateFlow, and Compose imports. TitleField is the stateless component in the Compose course lesson. The caller must obtain QueryViewModel through the correct owner and saved-state creation path. This is not a complete application.
Reload current truth and handle absence
A restored id can point to a deleted row or a different account scope. Validate it and query current authorized storage rather than restoring an old full object as authority. Persist confirmed edits when committed, not only in onDestroy, because that callback is not guaranteed before process termination. If a valuable draft must survive task dismissal or a fresh session, save it durably with an explicit retention policy. Saved-state APIs are a recovery aid and should not be described as a universal persistence guarantee.
Verify the fix
First reproduce rotation and verify the value remains correct without duplicate loads or events. Separately test system process recreation in a controlled Android environment while preserving the task, then resume and verify selection, draft policy, and current database content. Test task dismissal and force-stop as different scenarios with explicit expectations; neither should be used as proof of ordinary system-restoration behavior.
Keep it from recurring
- Document state lifetimes beside the feature contract. Keep values flowing from one owner, use stable list and navigation identities, and include recreation plus process-recovery scenarios when state ownership changes. Test missing records and account changes during restoration as well as the successful case.