@Serializable
public final class NavBackStack<T extends NavKey> implements MutableList, StateObject


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.samples.NavBackStackSamples.MultiBackStack

val multiBackStack =
    rememberSerializable(serializer = serializer()) {
        MultiBackStack(
            homeTabStack = NavBackStack(),
            chatTabStack = NavBackStack(),
            spacesTabStack = NavBackStack(),
        )
    }
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 methods

From kotlin.collections.List
boolean
contains(@NonNull T element)
boolean
@NonNull T
get(int index)
int
int
indexOf(@NonNull T element)
boolean
@NonNull Iterator<@NonNull T>
int
lastIndexOf(@NonNull T element)
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)
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
@NonNull StateRecord

The first state record in a linked list of state records.

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)