How do I save/restore a `Nav3` `NavBackStack` myse...
# compose
u
How do I save/restore a
Nav3
NavBackStack
myself (i.e. not using the
rememberNavBackStack
-- without resorting to having keys be
Parcelable
? (and keeping the @Serializable). I'm poking inside the
rememberNavBackStack
but it uses some
Saver
, not sure how would I pass that into
outState: Bundle
that
onSaveInstanceState
provides?
i
The SavedState Release Notes talk about how SavedState integrates with KotlinX Serialization, which is what
NavBackStack
uses. In places like your activity, you can use the
by saved
which does all the work for you:
Copy code
val backStack by saved { NavBackStack() }
You should probably stop overriding most methods in your activity at all, you don't really need to do that in 2026 anymore 🙂
thank you color 1
u
Copy code
private val backStack by saved(serializer = NavBackStackSerializer(NavKeySerializer())) {
    NavBackStack()
}
for completeness, otherwise runtime crash