RememberNavBackStackKt

Added in 1.0.0-alpha11

public final class RememberNavBackStackKt


Summary

Public methods

static final @NonNull NavBackStack<@NonNull NavKey>

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

static final @NonNull NavBackStack<@NonNull NavKey>
@Composable
rememberNavBackStack(
    @NonNull SavedStateConfiguration configuration,
    @NonNull NavKey... elements
)

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

Public methods

rememberNavBackStack

@Composable
public static final @NonNull NavBackStack<@NonNull NavKeyrememberNavBackStack(@NonNull NavKey... elements)

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 SerializersModule.

import androidx.navigation3.runtime.rememberNavBackStack import androidx.navigation3.runtime.samples.RememberNavBackStackSamples.Details import androidx.navigation3.runtime.samples.RememberNavBackStackSamples.Home // Works on Android (uses reflection internally). rememberNavBackStack(Home("start"), Details(42))
Parameters
@NonNull NavKey... elements

The initial keys of this back stack.

Returns
@NonNull NavBackStack<@NonNull NavKey>

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

rememberNavBackStack

@Composable
public static final @NonNull NavBackStack<@NonNull NavKeyrememberNavBackStack(
    @NonNull SavedStateConfiguration configuration,
    @NonNull NavKey... elements
)

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.

import androidx.navigation3.runtime.rememberNavBackStack import androidx.navigation3.runtime.samples.RememberNavBackStackSamples.Details import androidx.navigation3.runtime.samples.RememberNavBackStackSamples.Home import androidx.navigation3.runtime.samples.RememberNavBackStackSamples.Screen import androidx.savedstate.serialization.SavedStateConfiguration val config = SavedStateConfiguration {  // Register subtypes for open polymorphism or multiplatform use.  serializersModule = SerializersModule {  polymorphic(baseClass = Screen::class) {  subclass(serializer = Home.serializer())  subclass(serializer = Details.serializer())  }  } } // Pass the configuration so encoding/decoding works consistently. rememberNavBackStack(config, Home("start"))
Parameters
@NonNull SavedStateConfiguration configuration

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

@NonNull NavKey... elements

The initial NavKey elements of this back stack.

Returns
@NonNull NavBackStack<@NonNull NavKey>

A NavBackStack that survives process death and configuration changes.

Throws
kotlin.IllegalArgumentException

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