Skip to content

navigation-compose-util

Maven Central

Static Badge Static Badge Static Badge Static Badge

v1.0.0

Integration of the Navigation Compose library with some useful utilities to simplify the sharing of the data between the destinations during the navigation

Implementation

Version catalog

  • libs.version.toml
[versions]
navigationCompose = "2.9.1"
navigationComposeUtil = "1.0.0"

[libraries]
navigation-compose = { module = "org.jetbrains.androidx.navigation:navigation-compose", version.ref = "navigationCompose" }
navigation-compose-util = { module = "io.github.n7ghtm4r3:equinoxmisc-navigation-compose-util", version.ref = "navigationComposeUtil" }
  • build.gradle.kts
dependencies {
    implementation(libs.navigation.compose)
    implementation(libs.navigation.compose.util)
}

Gradle (Kotlin)

dependencies {
    implementation("org.jetbrains.androidx.navigation:navigation-compose:2.9.1")
    implementation("io.github.n7ghtm4r3:equinoxmisc-navigation-compose-util:1.0.0")
}

Gradle Groovy

dependencies {
    implementation 'org.jetbrains.androidx.navigation:navigation-compose:2.9.1'
    implementation 'io.github.n7ghtm4r3:equinoxmisc-navigation-compose-util:1.0.0'
}

Usage

With the navWithData method is possible to navigate to destinations and attach data that those destinations will use when visible (on top of the stack)

val navigator: NavHostController // an instance of the NavHostController

// attach data during the navigation
navigator.navWithData(
    route = "Home",
    data = buildMap {
        put("selectedTabTitle", "Settings")
    }
)

Note

In this example the destination has the String type, but this method has been overloaded with all the supported types by the original Jetbrains API such as NavUri, NavDeepLinkRequest, etc...

Retrieving navigation data

The library provides two APIs to retrieve the navigation data attached to the current destination: getDestinationNavData and getAllDestinationNavData

getDestinationNavData

@Composable
fun App() {
    // an instance of the NavHostController
    val navigator: NavHostController = rememberNavController()
    NavHost(
        navController = navigator,
        startDestination = "Splashscreen"
    ) {
        composable(
            route = "Splashscreen"
        ) {
            val splashscreen = equinoxScreen { Splashscreen() }
            splashscreen.ShowContent()
        }
        composable(
            route = "Home"
        ) {
            val selectedTabTitle: String? = navigator.getDestinationNavData(
                key = "selectedTabTitle",
                defaultValue = // custom value if no data has been shared with that key
            )
            val home = equinoxScreen() {
                Home(
                    selectedTabTitle = selectedTabTitle
                )
            }
            home.ShowContent()
        }
    }
}

getAllDestinationNavData

@Composable
fun App() {
    // an instance of the NavHostController
    val navigator: NavHostController = rememberNavController()
    NavHost(
        navController = navigator,
        startDestination = "Splashscreen"
    ) {
        composable(
            route = "Splashscreen"
        ) {
            val splashscreen = equinoxScreen { Splashscreen() }
            splashscreen.ShowContent()
        }
        composable(
            route = "Home"
        ) {
            val navData: Map<String, Any?> = navigator.getAllDestinationNavData()
            // your logic to use the navData
        }
    }
}

Note

The all destination data means just to the data shared with the visible destination

Removing navigation data

The library provides two APIs to remove the navigation data attached to the last destination shown before the NavHostController performed the popBackStack: clearLastDestinationNavData and clearLastDestinationAllNavData

Note

Both of these methods must be invoked from the destination which navigated to the destination owner of the data to clear, for example Splashscreen navigates to Home, the navigation data have to be cleaned from the Splashscreen destination

clearLastDestinationNavData

@Composable
fun App() {
    // an instance of the NavHostController
    val navigator: NavHostController = rememberNavController()
    NavHost(
        navController = navigator,
        startDestination = "Splashscreen"
    ) {
        composable(
            route = "Splashscreen"
        ) {
            LaunchedEffect(Unit) {
                val removedItems = navigator.clearLastDestinationNavData(
                    key_one, key_two
                )
                // will be removed just the data specified by the specified keys
            }

            val splashscreen = equinoxScreen { Splashscreen() }
            splashscreen.ShowContent()
        }
        composable(
            route = "Home"
        ) {
            // home content
        }
    }
}

clearLastDestinationAllNavData

@Composable
fun App() {
    // an instance of the NavHostController
    val navigator: NavHostController = rememberNavController()
    NavHost(
        navController = navigator,
        startDestination = "Splashscreen"
    ) {
        composable(
            route = "Splashscreen"
        ) {

            LaunchedEffect(Unit) {
                val removedItems = navigator.clearLastDestinationAllNavData()
                // will be removed all the navigation data attached to the destination
            }

            val splashscreen = equinoxScreen { Splashscreen() }
            splashscreen.ShowContent()
        }
        composable(
            route = "Home"
        ) {
            // home content
        }
    }
}