Android Compose NavHost
在 Compose 层次结构中的导航函数,相当于xml >> Navigation组件。
implementation 'androidx.navigation:navigation-compose:2.4.1'
别导错了导成 androidx.navigation:navigation-runtime-ktx:x.x.x,因为它也有 NavHost 而且 Android Studio 优先导这个
sealed class Screen(val route: String) { object Profile : Screen("profile") object Dashboard : Screen("dashboard") object Scrollable : Screen("scrollable") object Dialog : Screen("dialog/{title}") { fun createRoute(title: String) = "dialog/$title" } } @Composable private fun AppView() { val navController: NavHostController = rememberNavController() Scaffold { innerPadding -> NavHost(navController, Screen.Profile.route, Modifier.padding(innerPadding)) { composable(Screen.Profile.route) { backStackEntry -> ProfileView(navController,backStackEntry) } composable(Screen.Dashboard.route) { backStackEntry -> DashboardView(navController,backStackEntry) } composable(Screen.Scrollable.route) { backStackEntry -> ScrollableView(navController,backStackEntry) } dialog(Screen.Dialog.route) { backStackEntry -> DialogContent(navController,backStackEntry) } } } } @Composable fun ScrollableView(navController: NavHostController,backStackEntry: NavBackStackEntry) { Column(Modifier.fillMaxSize()) { OutlinedButton(onClick = { //防止重复调用 if(backStackEntry.lifecycle.currentState == Lifecycle.State.RESUMED) navController.navigate(Screen.Dialog.createRoute(Uri.encode("这是另一个提示"))) }) { Text(text = "这是另一个提示") } OutlinedButton(onClick = { navController.popBackStack() }, Modifier.align(Alignment.CenterHorizontally)) { Text(text = "to Back") } } } @Composable fun DashboardView(navController: NavHostController,backStackEntry: NavBackStackEntry) { Column(Modifier.fillMaxSize()) { OutlinedButton(onClick = { //防止重复调用 if(backStackEntry.lifecycle.currentState == Lifecycle.State.RESUMED) navController.navigate(Screen.Dialog.createRoute(Uri.encode("这是一个提示"))) }) { Text(text = "这是一个提示") } OutlinedButton(onClick = { navController.popBackStack() }, Modifier.align(Alignment.CenterHorizontally)) { Text(text = "to Back") } } } @Composable fun ProfileView(navController: NavHostController,backStackEntry: NavBackStackEntry) { Column(Modifier.fillMaxSize()) { OutlinedButton(onClick = { //防止重复调用 if(backStackEntry.lifecycle.currentState == Lifecycle.State.RESUMED) navController.navigate(Screen.Dialog.createRoute(Uri.encode("这是一个警告"))) }, Modifier.align(Alignment.CenterHorizontally)) { Text(text = "这是一个警告") } OutlinedButton(onClick = { //防止重复调用 if(backStackEntry.lifecycle.currentState == Lifecycle.State.RESUMED) navController.navigate(Screen.Dashboard.route) }, Modifier.align(Alignment.CenterHorizontally)) { Text(text = "to DashboardView") } OutlinedButton(onClick = { //防止重复调用 if(backStackEntry.lifecycle.currentState == Lifecycle.State.RESUMED) navController.navigate(Screen.Scrollable.route) }, Modifier.align(Alignment.CenterHorizontally)) { Text(text = "to ScrollableView") } } } @Composable fun DialogContent(navController: NavHostController,backStackEntry: NavBackStackEntry) { AlertDialog( onDismissRequest = {}, title = { Text(text = backStackEntry.arguments?.getString("title","")?: "")}, confirmButton = { TextButton(onClick = { navController.popBackStack() }) { Icon(Icons.Filled.Done, contentDescription = null ) } } ) }
fun NavHost(
navController: NavHostController!,//NavHostController实例
startDestination: String!,//默认显示堆栈
modifier: Modifier! = Modifier,
route: String? = null,
builder: (@ExtensionFunctionType NavGraphBuilder.() -> Unit)?
): Unit
判断backStackEntry是否在RESUMED(前台)防止重复调用.
navController.popBackStack() 返回上一堆栈
navController.navigate(XXX) 跳转到指定堆栈