First reproduce the difference between activity recreation and process recreation. A cart surviving rotation only demonstrates that an in-memory owner may have retained it; it does not prove persistence.
Use three layers:
On restoration, reload the cart by ID and recalculate availability and prices. Do not restore an old total as if it were authoritative.
This ViewModel fragment keeps a restoration key rather than serializing the entire cart:
class CheckoutViewModel(
private val savedState: SavedStateHandle
) : ViewModel() {
val cartId = savedState.getStateFlow<String?>("cartId", null)
fun selectCart(id: String) {
savedState["cartId"] = id
}
}
The repository observes or reloads the persisted cart for this ID. Never put large images or a complete response graph into saved state.
Add items, background the app, terminate its background process, and reopen its retained task. Separately test normal recreation and a fresh launch. Force-stop and task dismissal are different scenarios; saved state is not a durable storage guarantee.
What if the cart was deleted remotely? Render an explicit expired-cart state with a recovery action instead of repeatedly retrying or showing stale purchase details.
Mark this when you can explain the answer in your own words.
Help fellow developers prepare for interviews
Sharing helps the Android community grow ๐