One Compose Navigation Trap: Duplicated Back Stack After Process Death
If process recreation returns users to the wrong screen path, your navigation state is being rebuilt twice. This commonly happens when both route arguments and saved state replay the same transition.
Step 1: keep canonical route state in one source
data class RouteState(
val graph: String,
val args: Bundle?
)
Step 2: restore stack once from SavedStateHandle
val restored = handle.get<RouteState>("nav_state")
if (restored != null) navController.restore(restored)
Step 3: guard against duplicate navigate calls on first composition
if (!restoredOnce) {
restoredOnce = true
navController.navigate(startRoute)
}
Pitfall
Navigation calls inside recomposing UI blocks with no one-shot guard.
Verification
- Process death restore returns to expected nested screen.
- Back button pops one level at a time (no duplicates).
- Nav tests pass across rotation and kill/restart flows.