RememberNavBackStackKt

Added in 1.0.0-alpha10

public final class RememberNavBackStackKt


Summary

Public methods

static final @NonNull NavBackStack<@NonNull NavKey>
@Composable
<T extends NavKey> rememberNavBackStack(@NonNull T... elements)

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

static final @NonNull NavBackStack<@NonNull NavKey>
@Composable
<T extends NavKey> rememberNavBackStack(
    @NonNull SavedStateConfiguration configuration,
    @NonNull T... 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 NavKey> <T extends NavKey> rememberNavBackStack(@NonNull T... 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 T... 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 NavKey> <T extends NavKey> rememberNavBackStack(
    @NonNull SavedStateConfiguration configuration,
    @NonNull T... 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.

Serialization requirements

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

  • For closed polymorphism (sealed hierarchies), the compiler knows all subtypes and generates serializers automatically. No custom SavedStateConfiguration is required.

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

    • On Android, SavedStateConfiguration.DEFAULT uses a reflective serializer that can handle subtypes without registration.

    • On other platforms, or when you supply a custom configuration, you must register all subtypes of NavKey in a SerializersModule and pass that via configuration. You must also provide the same configuration to the encoder/decoder when saving/restoring state.

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<Screen>(config, Home("start"))
Parameters
@NonNull SavedStateConfiguration configuration

Controls how element serializers are resolved. On Android, SavedStateConfiguration.DEFAULT uses reflection; otherwise, the provided SerializersModule is used.

@NonNull T... elements

The initial keys of this back stack.

Returns
@NonNull NavBackStack<@NonNull NavKey>

A NavBackStack that survives process death and configuration changes.