NavBackStack
-
Cmn
@Serializable
class NavBackStack<T : NavKey> : 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.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 : NavKey> NavBackStack() |
Cmn
|
<T : NavKey> NavBackStack(base: SnapshotStateList<T>)Creates a new back stack backed by the provided |
Cmn
|
<T : NavKey> NavBackStack(vararg elements: T) |
Cmn
|
Inherited functions |
|||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||
|
Inherited properties |
|||
|---|---|---|---|
|
Public constructors
NavBackStack
<T : NavKey> NavBackStack(base: SnapshotStateList<T>)
Creates a new back stack backed by the provided SnapshotStateList.