Navigation
Use salt-ui-navigation for a Navigation 3 back and forward stack. Opt in with @OptIn(UnstableSaltUiApi::class).
kotlin
implementation("io.github.moriafly:salt-ui-navigation:<TAG>")SaltNavigator
The last entry in the back stack is the visible route. Back moves that route into the forward stack. Compose observes changes to both stacks, so you can update the available navigation controls.
Navigation behavior
navigate(route)leaves the stacks alone if the route is already visible. Otherwise, it clears forward history. When the route is intopLevelRoutes, it replaces the root and clears the rest of the back stack. Other routes are pushed.back()moves the current entry to the forward stack if there is a route beneath it.forward()restores the most recently popped route if one exists.
rememberSaltNavigator
kotlin
@Composable
fun rememberSaltNavigator(
configuration: SavedStateConfiguration,
initRoute: NavKey,
topLevelRoutes: Set<NavKey>
): SaltNavigatorThe back stack uses rememberNavBackStack. The forward stack is saved with rememberSaveable and a custom saver. Include polymorphic serializers in configuration for every NavKey that can appear in either stack. initRoute must be in topLevelRoutes.
Example
kotlin
@OptIn(UnstableSaltUiApi::class)
@Composable
fun App() {
val navigator = rememberSaltNavigator(
configuration = SavedStateConfiguration {
polymorphic(NavKey::class) {
subclass(Home::class)
subclass(Library::class)
subclass(Settings::class)
}
},
initRoute = Home,
topLevelRoutes = setOf(Home, Library, Settings)
)
// ...
}See the shared sample navigation for registration of real destinations.