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.
1
val stack = mutableStateStackOf("πŸ‡", "πŸ‰", "🍌", "🍐", "πŸ₯", "πŸ‹")
2
// πŸ‡, πŸ‰, 🍌, 🍐, πŸ₯, πŸ‹
3
​
4
stack.lastItemOrNull
5
// πŸ‹
6
​
7
stack.push("🍍")
8
// πŸ‡, πŸ‰, 🍌, 🍐, πŸ₯, πŸ‹, 🍍
9
​
10
stack.pop()
11
// πŸ‡, πŸ‰, 🍌, 🍐, πŸ₯, πŸ‹
12
​
13
stack.popUntil { it == "🍐" }
14
// πŸ‡, πŸ‰, 🍌, 🍐
15
​
16
stack.replace("πŸ“")
17
// πŸ‡, πŸ‰, 🍌, πŸ“
18
​
19
stack.replaceAll("πŸ’")
20
// πŸ’
Copied!
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.
Last modified 3mo ago