SavedStateHandle and process reconstruction
Rebuild, do not serialize the world
SavedStateHandle uses saved-state Bundle machinery. Store small inputs needed to recreate screen state: IDs, query, selected tab, small draft fields, and scroll/navigation anchors.
class SearchViewModel(
private val saved: SavedStateHandle,
private val repository: SearchRepository,
) : ViewModel() {
val query = saved.getStateFlow("query", "")
val results = query
.debounce(300)
.flatMapLatest(repository::search)
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList())
fun updateQuery(value: String) { saved["query"] = value }
}
After process recreation, query returns and results reload from authoritative data.
What not to save
- database result lists or bitmaps
- Retrofit responses/large Parcelable graphs
- repository/cache objects
- coroutine jobs, callbacks, open files
- secrets/tokens that belong in secure durable storage
Large serialization occurs on the main thread and can cause jank or transaction-size failures.
Navigation arguments are reconstruction inputs
Pass a product ID, not Product. A deep link or restored back stack can recreate the destination independently; repository reload handles fresh/deleted/offline states.
sealed interface ProductUiState {
data object Loading : ProductUiState
data class Ready(val product: ProductUi) : ProductUiState
data object NotFound : ProductUiState
data class Failed(val retryable: Boolean) : ProductUiState
}
Important timing corner case
Saved-state registry captures writes when the host stops. Writes performed while already stopped are not necessarily captured unless it starts and stops again. Critical background business state therefore belongs in durable storage, not SavedStateHandle.
Test matrix
- Rotation/configuration recreation
- Destination remains on back stack then returns
- System process death with task restoration
- User removes task/force stops (saved state should not be treated as durable)
- Argument references deleted or unauthorized data
- Schema/app upgrade with old durable records
SavedStateHandle is a reconstruction cache tied to saved task state. It is not a database, backup system, or guarantee after explicit task removal.
Sources
Interview exercises
- Choose what to save for a 12-field checkout form.
- Recover a product route after its entity was deleted.
- Explain why background upload progress cannot live only in SavedStateHandle.