rememberNavBackStack

Provides a NavBackStack that is automatically remembered in the Compose hierarchy across process death and configuration changes.

This overload does not take a SavedStateConfiguration. It relies on the platform default: on Android, state is saved/restored using a reflection-based serializer; on other platforms this will fail at runtime. If you target non-Android platforms, use the overload that accepts a SavedStateConfiguration and register your serializers explicitly.

When to use this overload

  • You are on Android only and want a simple API that uses reflection under the hood.

  • Your back stack elements use closed polymorphism (sealed hierarchies) or otherwise work with Android’s reflective serializer.

Serialization requirements

  • Each element placed in the NavBackStack must be @Serializable.

  • For closed polymorphism (sealed hierarchies), the compiler knows all subtypes and generates serializers; Android’s reflection will also work.

  • For open polymorphism (interfaces or non-sealed hierarchies):

    • On Android, the reflection path can handle subtypes without manual registration.

    • On non-Android, this overload is unsupported; use the configuration overload and register all subtypes of NavKey in a kotlinx.serialization.modules.SerializersModule.

Return

A NavBackStack that survives process death and configuration changes on Android.

Parameters

elements

The initial keys of this back stack.

See also

Samples

androidx.navigation3.runtime.samples.rememberNavBackStack_withReflection

Provides a NavBackStack that is automatically remembered in the Compose hierarchy across process death and configuration changes.

This function uses NavBackStackSerializer under the hood to save and restore the back stack via rememberSerializable. It is designed specifically for open polymorphism of the NavKey type.

Serialization requirements

  • All destination keys must be @Serializable and implement the NavKey interface.

  • You must provide a SavedStateConfiguration containing a SerializersModule that registers all subtypes of NavKey. This is required to handle open polymorphism correctly across all platforms.

On Android, an overload of this function is available that does not require a SavedStateConfiguration. That version uses reflection internally and does not require subtypes to be registered, but it is not available on other platforms.

Return

A NavBackStack that survives process death and configuration changes.

Parameters

configuration

The SavedStateConfiguration containing a SerializersModule configured for NavKey polymorphism. This configuration must be provided and cannot be the default.

elements

The initial NavKey elements of this back stack.

See also

Throws

If the provided configuration uses the default SerializersModule, as explicit NavKey subtype registration is required.

Samples

androidx.navigation3.runtime.samples.rememberNavBackStack_withSerializersModule