Код: Выделить всё
├── AGraph
│ ├── A1Screen
│ └── A2Screen
└── BGraph
├── B1Screen
└── B2Screen
< /code>
Демонстрационный код: < /p>
@Serializable
object AGraph
@Serializable
object A1
@Serializable
object A2
@Serializable
object BGraph
@Serializable
object B1
@Serializable
object B2
data class NavBarDestination(
val route: T,
val icon: ImageVector,
val label: String,
)
val navBarDestinations = listOf(
NavBarDestination(
route = AGraph,
icon = Icons.Default.Call,
label = "A",
),
NavBarDestination(
route = BGraph,
icon = Icons.Default.Settings,
label = "B",
),
)
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun OctopusApp() {
val navController = rememberNavController()
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentDestination = navBackStackEntry?.destination
Scaffold(
topBar = {
TopAppBar(
title = {
Text(stringResource(R.string.app_name))
},
navigationIcon = {
val canNavigateUp = navController.previousBackStackEntry != null
if (canNavigateUp) {
IconButton(onClick = navController::navigateUp) {
Icon(
imageVector = Icons.AutoMirrored.Filled.ArrowBack,
contentDescription = stringResource(R.string.up_button),
)
}
}
},
)
},
bottomBar = {
NavigationBar {
navBarDestinations.forEach { destination ->
NavigationBarItem(
selected = currentDestination?.hierarchy?.any {
it.hasRoute(destination.route::class)
} == true,
onClick = {
navController.navigate(destination.route) {
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
launchSingleTop = true
restoreState = true
}
},
icon = {
Icon(
imageVector = destination.icon,
contentDescription = destination.label,
)
},
label = { Text(destination.label) },
)
}
}
},
) { innerPadding ->
NavHost(
navController,
startDestination = AGraph,
modifier = Modifier
.fillMaxSize()
.padding(innerPadding),
) {
navigation(startDestination = A1) {
composable {
A1Screen(onClick = { navController.navigate(A2) })
}
composable {
A2Screen()
}
}
navigation(startDestination = B1) {
composable {
B1Screen(onClick = { navController.navigate(B2) })
}
composable {
B2Screen()
}
}
}
}
}
Когда я нажимаю между элементами в нижней навигационной панели, задний стек фактически растет точно на один пункт назначения (даже с этим всплывающимся действием в NavigationBaritem OnClick обратный вызов). В результате, случай NavController.previousbackStackentry! = Null запускается, и кнопка UP появляется в Topappbar , что не является желаемым поведением для первичных направлений.
Код: Выделить всё
TopAppBar
Есть ли канонический способ реализации такого поведения? Действительно ли мне нужно внедрить пользовательские несколько спинков (тогда зачем нам эти вложенные навигационные графики вообще?)?
Подробнее здесь: https://stackoverflow.com/questions/797 ... n-behavior