Compose Nav is a robust library designed to simplify and enhance navigation management within Jetpack Compose applications. By leveraging a clean and intuitive API, Compose Nav allows developers to organise and manage their app navigation effortlessly, ensuring a seamless user experience and maintainable codebase.
-
Add the Compose Nav dependency to your project's
build.gradlefile.dependencies { implementation "tech.kotlinlang:navigation:<version>" }dependencies { implementation("tech.kotlinlang:navigation:<version>") } -
Use
UiNavigationwhich helps to create a navigtation group or screen.UiNavigation( modifier = Modifier .fillMaxSize(), screenInfoGroup = ScreenInfoType.Group( startDestinationInfo = MainScreenInfo.SplashScreenInfo, screenInfoList = MainScreenInfo.all, ) ) -
Create
MainScreenInfo.ktand add all screen information usingScreenInfoclass.object MainScreenInfo { val SplashScreenInfo = ScreenInfo( name = "splashScreen", screenInfoType = ScreenInfoType.Screen { SplashScreen() }, ) val DashBoardScreenInfo = ScreenInfo( name = "dashboardScreen", screenInfoType = ScreenInfoType.Screen { DashBoardScreen() }, ) val all get() = listOf(SplashScreenInfo, DashBoardScreenInfo) } -
Now we need to navigate from one screen to another and which needs a nav controller and so we can access the same using
currentNavControllerOrThrow.// currentNavControllerOrThrow can only be accessed inside composable function val navController = currentNavControllerOrThrow navController.pushNew(MainScreenInfo.DashBoardScreenInfo)
| S. No. | Function name | Parameters | Description |
|---|---|---|---|
| 1 | pushNew | ScreenInfo | Push the given screenInfo to current screen and push the current screen into backstack |
| 2 | pushNewWithData | ScreenInfo, map | Works same as pushNew but also helps to send extra information to next screen |
| 3 | pushNewAndClear | ScreenInfo | Works same as pushNew with a difference that this remove your current screen and all previous screens from stack |
| 4 | pushNewWithDataAndClear | ScreenInfo, map | Works same as pushNewWithData with a difference that this remove your current screen and all previous screens from stack |
| 5 | popBack | -- | Remove the current screen from top of stack and navigate to previous screen if exists |
| 6 | popBack | -- | Remove the current screen from top of stack and navigate to previous screen if exists |
| 7 | popBackWithData | key, value | Works same as popBack but also helps to send some data back to previous screen |
| 8 | accessDataAndClear | key | This function helps us to retrieve data which is shared from previous screen which is closed using popBackWithData. How to use is shown below * |
val uiController = currentNavControllerOrThrow
LaunchedEffet(Unit) {
val isSucceded = uiController.accessDataAndClear<Boolean>("success")
}