@Serializable(with = NavBackStackSerializer)
public final class NavBackStack<T extends NavKey> implements MutableList, StateObject, RandomAccess


A mutable back stack of NavKey elements that integrates with Compose state.

This class wraps a SnapshotStateList so that updates to the stack automatically trigger recomposition in any observing Composables. It also implements StateObject, which allows it to participate in Compose's snapshot system directly.

Typically, you won’t construct a NavBackStack manually. Instead, prefer using rememberNavBackStack, which provides a stack that is automatically saved and restored across process death and configuration changes.

Example

val backStack = NavBackStack(Home("start"))
backStack += Details("item42") // pushes onto stack
backStack.removeLast() // pops stack
import androidx.compose.runtime.saveable.rememberSerializable
import androidx.navigation3.runtime.NavBackStack
import androidx.navigation3.runtime.NavKey
import androidx.navigation3.runtime.samples.NavBackStackSamples.Chat
import androidx.navigation3.runtime.samples.NavBackStackSamples.Home
import androidx.navigation3.runtime.samples.NavBackStackSamples.Spaces
import androidx.savedstate.serialization.SavedStateConfiguration

// https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/polymorphism.md#open-polymorphism
rememberSerializable(
    serializer = serializer(),
    configuration =
        SavedStateConfiguration {
            serializersModule = SerializersModule {
                polymorphic(baseClass = NavKey::class) {
                    subclass(clazz = Home::class)
                    subclass(clazz = Chat::class)
                    subclass(clazz = Spaces::class)
                }
            }
        },
) {
    NavBackStack<NavKey>()
}
import androidx.compose.runtime.saveable.rememberSerializable
import androidx.navigation3.runtime.NavBackStack
import androidx.navigation3.runtime.samples.NavBackStackSamples.SealedKey

// https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/polymorphism.md#closed-polymorphism
rememberSerializable(serializer = serializer()) { NavBackStack<SealedKey>() }
See also
rememberNavBackStack

for lifecycle-aware persistence.

Summary

Public constructors

<T extends NavKey> NavBackStack()

Creates a new back stack backed by the provided SnapshotStateList.

<T extends NavKey> NavBackStack(@NonNull T... elements)

Inherited fields

From kotlin.collections.List
int

Inherited methods

From kotlin.collections.List
@NonNull JsReadonlyArray<@NonNull T>
boolean
contains(@NonNull T element)
boolean
@NonNull T
get(int index)
int
indexOf(@NonNull T element)
boolean
int
lastIndexOf(@NonNull T element)
From kotlin.collections.MutableCollection
From kotlin.collections.MutableList
boolean
add(@NonNull T element)
void
add(int index, @NonNull T element)
boolean
boolean
addAll(int index, @NonNull Collection<@NonNull T> elements)
@NonNull JsArray<@NonNull T>
void
@NonNull ListIterator<@NonNull T>
@NonNull ListIterator<@NonNull T>
listIterator(int index)
boolean
remove(@NonNull T element)
boolean
@NonNull T
removeAt(int index)
boolean
@NonNull T
set(int index, @NonNull T element)
@NonNull List<@NonNull T>
subList(int fromIndex, int toIndex)
From androidx.compose.runtime.snapshots.StateObject
abstract @NonNull StateRecord

A mutable back stack of NavKey elements that integrates with Compose state.

StateRecord
mergeRecords(
    @NonNull StateRecord previous,
    @NonNull StateRecord current,
    @NonNull StateRecord applied
)

Produce a merged state based on the conflicting state changes.

void

Add a new state record to the beginning of a list.

Public constructors

public <T extends NavKey> NavBackStack()
public <T extends NavKey> NavBackStack(@NonNull SnapshotStateList<@NonNull T> base)

Creates a new back stack backed by the provided SnapshotStateList.

public <T extends NavKey> NavBackStack(@NonNull T... elements)