[英]Jetpack Compose + Navigation - Nested navigation with BottomNavBar
I'm trying to implement the following screen flow using Jetpack Compose + Jetpack Navigation:我正在尝试使用 Jetpack Compose + Jetpack Navigation 实现以下屏幕流程:
Actually, i am able to code two singles cases:实际上,我可以编写两个单例:
I'm not able to code the whole problem.我无法对整个问题进行编码。 In fact, i have an issue with the management of the NavHost.
事实上,我对 NavHost 的管理有疑问。 In the first case (SplashScreen -> HomeScreen) i need to call the NavHost at a high scope:
在第一种情况下(SplashScreen -> HomeScreen)我需要在 scope 高位调用 NavHost:
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyAppTheme {
//init the Navigation Controller for screen navigation
val navController = rememberNavController()
//setup the Navigation Graph
SetupNavGraph(navController)
while in the second case i need to call it in the innerPadding
scope of the Scaffold
composable:而在第二种情况下,我需要在
Scaffold
可组合项的innerPadding
scope 中调用它:
fun MainScreen(navController: NavHostController) {
Scaffold(
bottomBar = {
BottomNavBar(navController)
}
) { //innerPadding scope
//setup the Navigation Graph
SetupNavGraph(navController)
}
}
Please assume that SetupNavGraph()
function works as intended (call NavHost
to generate the navigation tree)请假设 SetupNavGraph
SetupNavGraph()
function 按预期工作(调用NavHost
生成导航树)
NavHost
without success.NavHost
但没有成功。NavHost
in setContent()
i'm able to load the splashscreen and move to an empty BottomNavBar
screen.setContent()
中设置NavHost
,我就可以加载闪屏并移动到空的BottomNavBar
屏幕。 If i click on the BottomNavElements
i'm able to navigate to the child tabs (in the example above "Favorite","Music","Places", "News") but the BottomNavBar
disappearsBottomNavElements
,我可以导航到子选项卡(在上面的示例中“收藏夹”、“音乐”、“地点”、“新闻”)但是BottomNavBar
消失了NavHost
in the innerPadding
scope because this is loaded only after switching to the main screen (in the example above "Favorite Tab" + BottomBarNav)innerPadding
scope 中设置NavHost
,因为它仅在切换到主屏幕后才加载(在上面的示例中“Favorite Tab”+ BottomBarNav) The only workaround i found is generating the BottomNavBar
composable in each of the BottomNav child tabs, but this generates a visible transition effect that i would like to avoid and, generally, doesn't seem a good practice.我发现的唯一解决方法是在每个 BottomNav 子选项卡中生成
BottomNavBar
可组合项,但这会生成我想避免的可见过渡效果,而且通常这似乎不是一个好的做法。
Ok, i found a solution.好的,我找到了解决方案。 This are the steps to achieve the desired result:
这是实现预期结果的步骤:
const val ROOT_ROUTE = "root"
@Composable
fun SetupRootNavGraph(navController: NavHostController) {
NavHost(
navController = navController,
startDestination = Screen.FirstScreen.route,
route = ROOT_ROUTE
) {
composable(Screen.FirstScreen.route) { FirstScreen(navController)}
composable(Screen.SecondScreen.route) { MainScreen(navController)}
}
}
const val BOTTOM_BAR_ROUTE = "bottomBar"
@Composable
fun SetupNavGraphBottomBar(navController: NavHostController) {
NavHost(
navController = navController,
startDestination = BottomBarScreen.FirstElement.route,
route = BOTTOM_BAR_ROUTE
) {
composable(BottomBarScreen.FirstElement.route) { FirstElementScreen() }
composable(BottomBarScreen.SecondElement.route) { SecondElementScreen() }
composable(BottomBarScreen.ThirdElement.route) { ThirdElementScreen() }
}
}
NavController
and the RootNavGraph
after setContent()
in your MainActivity
.MainActivity
中的setContent()
之后初始化NavController
和RootNavGraph
。 This will be in charge of the SplashScreen -> MainScreen navigation tree.class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyAppTheme {
//init the Navigation Controller for screen navigation
val navController = rememberNavController()
//setup the Root Navigation Graph
SetupRootNavGraph(navController)
}
}
}
}
NavController
in the Screen where you have your BottomNavBar ("MainScreen" in the example) and the assign to it the BottomNavGraph
in the innerPadding
scope.NavController
,并在innerPadding
BottomNavGraph
中为其分配 BottomNavGraph。@Composable
fun MainScreen(navController: NavHostController) {
//Re-initialize the NavController to set a new NavGraph
val navControllerBottomBar = rememberNavController()
Scaffold(
bottomBar = {
BottomNavBar(navControllerBottomBar)
}
) {
//setup the Navigation Graph
SetupNavGraphBottomBar(navControllerBottomBar, user)
}
}
And this will work like charm!这会很有魅力! Of course you will need to structure your
BottomNavBar
in order to manage the navigation as documented on Official docs当然,您需要构建
BottomNavBar
以管理官方文档中记录的导航
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.