Skip to content

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.

  • navigate(route) leaves the stacks alone if the route is already visible. Otherwise, it clears forward history. When the route is in topLevelRoutes, 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>
): SaltNavigator

The 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.

Google/Android 是 Google LLC 的注册商标,Salt Player 和糖醋音乐均是寻浔科技(上海)有限公司在中华人民共和国的注册商标