Stack API

SnapshotStateStack

Voyager is backed by a SnapshotStateStack:
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: whenever push is called
  • Replace: whenever replace and replaceAll are called
  • Pop: whenever pop and popAll are called
  • Idle: default event
This is very useful for deciding which transition to make.

Sample

Source code here.
Copy link
On this page
SnapshotStateStack
Events
Sample