Lesson 4 of 9 · 26m
Activities, ViewModels, and Process Death Without Denial
Learn the Android process model, what survives which destruction path, and how to design restoration instead of hoping rotation is rare.
Android does not owe your process continued existence. The system can kill it in the background, recreate activities after rotation, resize windows, revoke permissions, or reclaim memory while the user answers a phone call. Teams that treat lifecycle as a nuisance ship apps that lose checkout state after a camera intent. The correct posture is cheerful paranoia: anything important must have a restoration story.
Rotation is only the visible destruction
Configuration changes destroy and recreate the activity by default. A ViewModel survives that recreation, which makes it ideal for screen state:
class CheckoutViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
private val draft = savedStateHandle.getStateFlow("draft", CheckoutDraft())
}But process death is different. The whole process can be killed and later restored with the task, intents, fragments, and saved state. A ViewModel instance from the old process cannot survive because the process is gone. SavedStateHandle, durable storage, or re-derivable server state are your real tools.
Know the storage ladder
remember recomposition only
rememberSaveable small UI state across recreation
ViewModel configuration-change survival, in-memory
SavedStateHandle small process-death restoration state
DataStore/Room/backend durable or authoritative stateDo not serialize an entire object graph into SavedStateHandle because it is convenient. It is meant for compact restoration keys and drafts, not a database smuggled through a bundle.
Lifecycle-aware collection is part of correctness
Collecting in the wrong layer causes duplicate collection after recreation:
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.uiState.collect { render(it) }
}
}In Compose, prefer collectAsStateWithLifecycle() for state flows. The point is not ceremonial correctness: collection should stop when the UI is not visible, and restart without duplicating network subscriptions or observers.
Process death testing is a feature
Turn on "Don't keep activities" for a rough signal, but also use the real thing: background the app, kill the process from adb, return through the launcher or recents, and inspect what is restored:
adb shell am kill com.example.shop
adb shell monkey -p com.example.shop 1Test drafts, selected tabs, scroll position, authentication expiry, pending permissions, and deep-link restoration. A screen that looks fine after rotation can still be broken after process death.
Field notes
onSaveInstanceStatebundles are small; large bitmaps or parcelable graphs can trigger transaction failures.- Camera, document picker, and permission results are classic process-death traps because the user leaves your app mid-flow.
- Do not keep activity references in singletons. That is not architecture; it is a memory leak with a lease.
- If restored state and server state conflict, define the authority deliberately instead of merging whichever object happened to load last.
Was this lesson helpful?