Created
August 15, 2021 11:26
-
-
Save theapache64/02f5eeff3f7049eac54b4a8f492fdfde to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@OptIn(ExperimentalMaterialNavigationApi::class) | |
@Composable | |
private fun AppNavigation() { | |
val navController = rememberNavController() | |
val bottomSheetNavigator = rememberBottomSheetNavigator() | |
navController.navigatorProvider += bottomSheetNavigator | |
ModalBottomSheetLayout( | |
bottomSheetNavigator = bottomSheetNavigator, | |
) { | |
NavHost(navController = navController, startDestination = Screen.A.route) { // Open 'A' | |
composable(Screen.A.route) { | |
Column( | |
modifier = Modifier | |
.fillMaxSize() | |
.background(Color.Red) | |
) { | |
Text(text = "I am A") | |
Counter() | |
Button(onClick = { | |
navController.navigate(Screen.B.route) // Open BottomSheet B | |
}) { | |
Text(text = "GO TO BS") | |
} | |
} | |
} | |
bottomSheet(Screen.B.route) { | |
Column( | |
modifier = Modifier | |
.fillMaxSize() | |
.background(Color.Green) | |
) { | |
Text(text = "I am B") | |
Counter() | |
Button(onClick = { | |
navController.navigate(Screen.C.route) // Open BottomSheet C | |
}) { | |
Text(text = "GO TO CS") | |
} | |
} | |
} | |
bottomSheet(Screen.C.route) { | |
Column( | |
modifier = Modifier | |
.fillMaxSize() | |
.background(Color.Blue) | |
) { | |
Text(text = "I am C") | |
Counter() | |
Button(onClick = { | |
navController.navigate(Screen.D.route) { // Open D | |
// but before that, pop all screens between `D` and `A` | |
popUpTo(Screen.A.route) { | |
saveState = true // and save their state | |
inclusive = true // including `A`'s | |
} | |
} | |
}) { | |
Text(text = "GO TO D") | |
} | |
} | |
} | |
composable(Screen.D.route) { | |
Column( | |
modifier = Modifier | |
.fillMaxSize() | |
.background(Color.Black) | |
) { | |
Text(text = "I am D") | |
Counter() | |
Button(onClick = { | |
navController.navigate(Screen.A.route) { // Open 'A' | |
// but before that, pop this screen from the stack | |
popUpTo(Screen.D.route) { | |
inclusive = true | |
} | |
// don't create new instance for `A` | |
launchSingleTop = true | |
// restore `A`'s state, including all screens and their state when... | |
// we popped `A` last time. | |
restoreState = true | |
} | |
}) { | |
Text(text = "GO BACK") | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment