Skip to content

Kotlin multi-platform application navigation library.

License

Notifications You must be signed in to change notification settings

chRyNaN/navigation

Repository files navigation

navigation

Kotlin multi-platform application navigation library. Supports Jetpack Compose.
GitHub tag (latest by date)

val navigator = rememberNavigatorByKey("Greeting") { key ->
    when (key) {
        "Greeting" -> Text("Hello")
        "Farewell" -> Text("Good-bye")
        else -> Text("Unexpected Key: $key")
    }
}

navigator.goTo("Farewell")

Usage

Navigation is handled differently for each platform and UI framework. This library provides some common navigation components that serve as a recommended structure, but each platform and UI framework is independently responsible for handling navigation and choosing whether to conform to the provided components.

Jetpack Compose

There are three different ComposeNavigators that can handle navigation in Jetpack Compose. Which one to use depends on the application needs and personal preference.

Navigate by content

This approach allows for specifying the @Composable content on demand, rather than up-front.

val navigator = rememberNavigatorByContent("Greeting") { Text("Hello") }

// The NavContainer will start by displaying the initial content, which in this case is "Hello".
NavContainer(navigator)

// The above NavContainer will display "Good-bye" after the following call:
navigator.goTo("Farewell") { Text("Good-bye") }

// Goes back to the initial content: "Hello":
navigator.goBack()
Navigate by key

This approach allows for specifying the @Composable content for each key up-front. Then navigation can be done by simply providing a key.

val navigator = rememberNavigatorByKey("Greeting") { key ->
    when (key) {
        "Greeting" -> Text("Hello")
        "Farewell" -> Text("Good-bye")
        else -> Text("Unexpected Key: $key")
    }
}

// The NavContainer will start by displaying the initial content, which in this case is "Hello"
NavContainer(navigator)

// The above NavContainer will display "Good Bye" after the following call:
navigator.goTo("Farewell")

// Goes back to the initial content: "Hello":
navigator.goBack()
Navigate by NavigationIntent

This approach is similar to the key approach, but the key is a NavigationIntent and the returned ComposeNavigator implements the NavigationEventNavigator interface from the core module.

val navigator = rememberNavigatorByIntent(HomeNavigationIntent.Greeting) { navigationIntent ->
    when (navigationIntent) {
        HomeNavigationIntent.Greeting -> Text("Hello")