Interview frequency: …
Short answer
A user closing ends the Activity and clears its objects and state by their own choice, while process death happens forcibly by the system to free memory, with the SavedState preserved to restore it later.
Explanation
When the app is in the background, the Low Memory Killer (LMK) may decide to kill the app's entire process to free RAM for another active app. When the user returns, the system creates a completely new process, and the old singleton and ViewModel objects are cleared from memory, and the system only passes the saved Bundle to onCreate and SavedStateHandle to rebuild the state.
Code example
// Test process death via adb:
// adb shell am kill com.example.app
class ProfileViewModel(savedStateHandle: SavedStateHandle) : ViewModel() {
// Restores the ID safely even after Process Death
val userId: StateFlow<String> = savedStateHandle.getStateFlow("user_id", "")
}Common mistakes
- Relying entirely on static singletons or the in-memory cache and assuming they always stay alive while the user navigates.
- Not testing the app's behavior under the process death scenario, leading to sudden crashes from null pointers on re-entry.