" /> " /> "/>

Kotlin multi-platform application navigation library.

Last update: Aug 4, 2022

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")
        HomeNavigationIntent.Farewell -> Text("Good-bye")
    }
}

// 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(HomeNavigationIntent.Farewell)

// Goes back to the initial content: "Hello":
navigator.goBack()
Nested navigation

Within the scope of the content blocks, you can access the navigator property which can be used for nested navigation:

val navigator = rememberNavigatorByKey("Hello") { key ->
    when (key) {
        "Greeting" -> Button("Hello") {
            navigator.goBack() // Safe access to the navigator property within this scope.
        }
        "Farewell" -> Text("Good-bye")
        else -> Text("Unexpected Key: $key")
    }
}
Key State changes

The key of the currently displayed @Composable can be accessed by the ComposeNavigator.currentKey property:

navigator.currentKey

To listen to changes to the current key, use the ComposeNavigator.keyChanges property along with the Flow<T>.collectAsState function:

val currentKey by navigator.keyChanges.collectAsState(initial = currentKey)

For convenience, there is an extension function that performs the above logic: ComposeNavigator.currentKeyAsState() This is especially useful when a @Composable needs to be updated when the key changes, for instance in a Bottom Navigation component:

val currentKey by navigator.currentKeyAsState()

BottomNavigation {
    listOf(ScreenIntent.ColorList, ScreenIntent.Palette).forEach {
        BottomNavigationItem(
            selected = currentKey == it,
            onClick = {
                navigator.goTo(it)
            }
        )
    }
}

Android

To create a Navigator use one the provided navigator() functions. For instance:

val navigator = navigator<NavigationIntent>(activity = activity, onGoTo = { navigationIntent ->
    activity.startActivity(...)
})

navigator.goBack()

Building the library

The library is provided through Repsy.io. Checkout the releases page to get the latest version.
GitHub tag (latest by date)

Repository

repositories {
    maven {
        url = uri("https://repo.repsy.io/mvn/chrynan/public")
    }
}

Dependencies

core

implementation("com.chrynan.navigation:navigation-core:VERSION")

compose

implementation("com.chrynan.navigation:navigation-compose:VERSION")

Documentation

More detailed documentation is available in the docs folder. The entry point to the documentation can be found here.

License

Copyright 2021 chRyNaN

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

GitHub

https://github.com/chRyNaN/navigation
You might also like...

Microservice-grpc-multi-language-example - gRPC communication on multiple language demonstration (spring kotlin, go, .NET core 6)

Microservice-grpc-multi-language-example - gRPC communication on multiple language demonstration (spring kotlin, go, .NET core 6)

What is this Microservice gRPC demonstration w/incrementing state in multiple la

Jul 5, 2022

A Gradle plugin providing various utility methods and common code required to set up multi-version Minecraft mods.

Essential Gradle Toolkit A Gradle plugin providing various utility methods and common code required to set up multi-version Minecraft mods via archite

Aug 10, 2022

Modular Android architecture which showcase Kotlin, MVVM, Navigation, Hilt, Coroutines, Jetpack compose, Retrofit, Unit test and Kotlin Gradle DSL.

SampleCompose Modular Android architecture which showcase Kotlin, MVVM, Navigation, Hilt, Coroutines, Jetpack compose, Retrofit, Unit test and Kotlin

Jul 29, 2022

Kotrlin Programming Language Cross-Platform Development which includes Android, iOS and Backend. Pretty much everwhere.

Kotrlin Programming Language Cross-Platform Development which includes Android, iOS and Backend. Pretty much everwhere.

Kotlin-Everywhere: Kotlin Programming Language Cross-Platform Development This is still a WIP but the idea is to create a tiny KOTLIN project that cou

Aug 9, 2022

Trace all binder-funcion calls on android-platform

Trace all binder-funcion calls on android-platform

BinderHackDemo trace all binder-funcion calls on android-platform 该demo展示了如何使用libbinderhack.so模块,trace-app自身进程binder调用情况 您可以通过该次提交,查看如何使用libbinderhack

Dec 25, 2021

Plannr is an organizational platform developed using Java, in the form of an Android app, that helps university students coordinate their everyday routine.

Plannr is an organizational platform developed using Java, in the form of an Android app, that helps university students coordinate their everyday routine.

Plannr Plannr is an organizational platform developed using Java, in the form of an Android app, that helps university students coordinate their every

Dec 30, 2021

Camunda Platform 7 WebApp Auto-Login

Camunda Platform 7 WebApp Auto-Login Auto-login feature for development Why should you use it? Because otherwise, you need to type again and again "ad

May 10, 2022

A platform to capture, visualize and evaluate multiple Coiffeur-Jass

A platform to capture, visualize and evaluate multiple Coiffeur-Jass

JassTracker Screenshots Development The easiest way to start all components is using the configured IntelliJ run configuration. There exists one confi

Aug 3, 2022

An e-commerce app which provide a new platform to order food items from various restaurants

An e-commerce app which provide a new platform to order food items from various restaurants

Food_App_Internshala An e-commerce app which provide a new platform to order food items from various restaurants. Splash and Login Page Opening of the

Apr 26, 2022
HackerNews with Kotlin Multi-platform mobile technology
HackerNews with Kotlin Multi-platform mobile technology

KNews The goal of this project is to build mobile apps that consumes HackerNews API with Kotlin Multi-Platform technology. About My idea is to build 2

May 27, 2022
Kotlin Multi Platform UI

Xeon UI (work-in-progress ?? ??️ ??‍♀️ ⛏ ) Development Version Release This Is Latest Release ~ In Development $version_release = ~ What's New?? * In

Oct 15, 2021
The home of the amigo-platform which serves as the main service for the amigo multimedia platform

amigo-platform This is the home of the amigo-platform which serves as the main service for the amigo multimedia platform. Authentication with JWT Toke

Nov 22, 2021
A complete Kotlin application built to demonstrate the use of Modern development tools with best practices implementation using multi-module architecture developed using SOLID principles
A complete Kotlin application built to demonstrate the use of Modern development tools with best practices implementation using multi-module architecture developed using SOLID principles

This repository serves as template and demo for building android applications for scale. It is suited for large teams where individuals can work independently on feature wise and layer wise reducing the dependency on each other.

Aug 18, 2022
A deep learning based mobile application for the multi-class classification of pneumonia into three categories via Chest X-rays

PneumoniaClassifier A deep learning based mobile application for the multi-class classification of pneumonia into three categories via Chest X-rays. W

Dec 15, 2021
Android Multi Theme Switch Library ,use kotlin language ,coroutine ,and so on ...
Android Multi Theme Switch Library ,use  kotlin language ,coroutine ,and so on ...

Magic Mistletoe Android多主题(换肤)切换框架 背景 时隔四年,在网易换肤之前的思路下,做了几点改进,现在完全通过反射创建View,并且在SkinLoadManager中提供一个configCustomAttrs以支持自定义View的属性插队替换 摈弃了之前的AsyncTask

Jun 17, 2022
Collection of Rewrite Recipes pertaining to the JHipster web application & microservice development platform
Collection of Rewrite Recipes pertaining to the JHipster web application & microservice development platform

Apply JHipster best practices automatically What is this? This project implements a Rewrite module that applies best practices and migrations pertaini

Mar 7, 2022
Multi-thread ZX0 data compressor in Kotlin

ZX0-Kotlin ZX0-Kotlin is a multi-thread implementation of the ZX0 data compressor in Kotlin. Requirements To run this compressor, you must have instal

Apr 14, 2022
For Kotlin with SpringBoot project that have multi-module-structure template

Goals kotlin + spring-boot + gradle + multi-module building Module-Structure ---root |--- src.main.kotlin.KotlinSpringbootMultiModuleTemplateAppl

Jul 24, 2022