Skip to content

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

Info

Source code here.