Stack API¶
SnapshotStateStack¶
Voyager is backed by a SnapshotStateStack:
- Implementation of Stack that can be observed and snapshot
- Internally uses a SnapshotStateList
- State-aware: content change triggers a recomposition
You will use it to navigate forward (push
, replace
, replaceAll
) and backwards (pop
, popAll
, popUntil
), but the SnapshotStateStack
can also be used as a regular collection outside the Navigator
.
val stack = mutableStateStackOf("🍇", "🍉", "🍌", "🍐", "🥝", "🍋")
// 🍇, 🍉, 🍌, 🍐, 🥝, 🍋
stack.lastItemOrNull
// 🍋
stack.push("🍍")
// 🍇, 🍉, 🍌, 🍐, 🥝, 🍋, 🍍
stack.pop()
// 🍇, 🍉, 🍌, 🍐, 🥝, 🍋
stack.popUntil { it == "🍐" }
// 🍇, 🍉, 🍌, 🍐
stack.replace("🍓")
// 🍇, 🍉, 🍌, 🍓
stack.replaceAll("🍒")
// 🍒
You can also create a SnapshotStateStack
through rememberStateStack()
, it will restore the values after Activity recreation.
Events¶
Whenever the content changes, the SnapshotStateStack
will emit a StackEvent
. Use the stack.lastEvent
to get the most recent one.
The available events are:
Push
: wheneverpush
is calledReplace
: wheneverreplace
andreplaceAll
are calledPop
: wheneverpop
andpopAll
are calledIdle
: default event
This is very useful for deciding which transition to make.
Sample¶
Info
Source code here.