# NavBackStack

Artifact: [androidx.navigation3:navigation3-runtime](https://developer.android.com/jetpack/androidx/releases/navigation3) [View Source](https://cs.android.com/search?q=file:androidx/navigation3/runtime/NavBackStack.kt+class:androidx.navigation3.runtime.NavBackStack) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/navigation3#1.0.0) Common/AllAndroid/JVM
- Cmn

  ```
  @Serializable(with = NavBackStackSerializer)
  class NavBackStack<T : NavKey> : MutableList<T>, StateObject, RandomAccess
  ```

*** ** * ** ***

A mutable back stack of `https://developer.android.com/reference/kotlin/androidx/navigation3/runtime/NavKey` elements that integrates with Compose state.

This class wraps a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateList` so that updates to the stack automatically trigger recomposition in any observing Composables. It also implements `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateObject`, which allows it to participate in Compose's snapshot system directly.

Typically, you won't construct a `https://developer.android.com/reference/kotlin/androidx/navigation3/runtime/NavBackStack` manually. Instead, prefer using `https://developer.android.com/reference/kotlin/androidx/navigation3/runtime/rememberNavBackStack.composable#rememberNavBackStack(androidx.savedstate.serialization.SavedStateConfiguration,kotlin.Array)`, which provides a stack that is automatically saved and restored across process death and configuration changes.

### Example

```kotlin
val backStack = NavBackStack(Home("start"))
backStack += Details("item42") // pushes onto stack
backStack.removeLast()         // pops stack
```

```kotlin
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>()
}
```

```kotlin
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 |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/navigation3/runtime/rememberNavBackStack.composable#rememberNavBackStack(androidx.savedstate.serialization.SavedStateConfiguration,kotlin.Array)` | for lifecycle-aware persistence. |

## Summary

| ### Public constructors |
|---|---|
| `<T : https://developer.android.com/reference/kotlin/androidx/navigation3/runtime/NavKey> https://developer.android.com/reference/kotlin/androidx/navigation3/runtime/NavBackStack#NavBackStack()()` | Cmn |
| `<T : https://developer.android.com/reference/kotlin/androidx/navigation3/runtime/NavKey> https://developer.android.com/reference/kotlin/androidx/navigation3/runtime/NavBackStack#NavBackStack(androidx.compose.runtime.snapshots.SnapshotStateList)(base: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateList<T>)` Creates a new back stack backed by the provided `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateList`. | Cmn |
| `<T : https://developer.android.com/reference/kotlin/androidx/navigation3/runtime/NavKey> https://developer.android.com/reference/kotlin/androidx/navigation3/runtime/NavBackStack#NavBackStack(kotlin.Array)(vararg elements: T)` | Cmn |

### Inherited functions

From [kotlin.collections.List](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html) `open operator https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/contains.html(element: T)` Cmn `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/contains-all.html(elements: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-collection/index.html<T>)` Cmn `open operator T` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/get.html(index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Cmn `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index-of.html(element: T)` Cmn `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/is-empty.html()` Cmn `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/last-index-of.html(element: T)` Cmn From [kotlin.collections.MutableCollection](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-collection/index.html) `open operator https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-iterator/index.html<T>` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-collection/iterator.html()` Cmn From [kotlin.collections.MutableList](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html) `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `@https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-ignorable-return-value/index.html
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/add.html(element: T)` Cmn `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/add.html(index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, element: T)` Cmn `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `@https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-ignorable-return-value/index.html
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/add-all.html(elements: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-collection/index.html<T>)` Cmn `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `@https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-ignorable-return-value/index.html
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/add-all.html(index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, elements: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-collection/index.html<T>)` Cmn `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/clear.html()` Cmn `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list-iterator/index.html<T>` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/list-iterator.html()` Cmn `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list-iterator/index.html<T>` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/list-iterator.html(index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Cmn `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `@https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-ignorable-return-value/index.html
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/remove.html(element: T)` Cmn `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `@https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-ignorable-return-value/index.html
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/remove-all.html(elements: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-collection/index.html<T>)` Cmn `open T` `@https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-ignorable-return-value/index.html
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/remove-at.html(index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Cmn `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `@https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-ignorable-return-value/index.html
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/retain-all.html(elements: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-collection/index.html<T>)` Cmn `open operator T` `@https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-ignorable-return-value/index.html
https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/set.html(index: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, element: T)` Cmn `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/index.html<T>` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-mutable-list/sub-list.html(fromIndex: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, toIndex: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Cmn From [androidx.compose.runtime.snapshots.StateObject](https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateObject) `open https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateRecord?` `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateObject#mergeRecords(androidx.compose.runtime.snapshots.StateRecord,androidx.compose.runtime.snapshots.StateRecord,androidx.compose.runtime.snapshots.StateRecord)(
previous: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateRecord,
current: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateRecord,
applied: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateRecord
)`

Produce a merged state based on the conflicting state changes.
Cmn `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateObject#prependStateRecord(androidx.compose.runtime.snapshots.StateRecord)(value: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateRecord)`

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

### Inherited properties

From [kotlin.collections.List](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html) `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/size.html` Cmn From [androidx.compose.runtime.snapshots.StateObject](https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateObject) `open https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateRecord` `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/StateObject#firstStateRecord()`

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

## Public constructors

### NavBackStack

Cmn

```
<T : NavKey> NavBackStack()
```

### NavBackStack

Cmn

```
<T : NavKey> NavBackStack(base: SnapshotStateList<T>)
```

Creates a new back stack backed by the provided `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateList`.

### NavBackStack

Cmn

```
<T : NavKey> NavBackStack(vararg elements: T)
```