我的应用程序在主活动中包含一个底部导航视图,其中包含 3 个菜单项,每个菜单项都会在导航容器视图中扩展各自的导航图。每个图都有 2 个或更多通过动作连接的片段。
这里的问题是在屏幕方向更改期间应用程序崩溃了。此外,底部导航不保留导航图的状态,并且没有为底部导航维护后台堆栈。
下面的代码示例。
val bottomNavigation = binding.bottom_navigation_view
navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
navController = navHostFragment.navController
bottomNavigation.setOnNavigationItemSelectedListener{ item ->
when(item.itemId){
R.id.navigation_home ->{
val navGraph = navController.navInflater.inflate(R.navigation.nav_graph)
navController.graph = navGraph
true
}
R.id.navigation_search ->{
val searchGraph=navController.navInflater.inflate(R.navigation.search_nav_graph)
navController.graph = searchGraph
true
}
R.id.navigation_about ->{
val infoGraph = navController.navInflater.inflate(R.navigation.info_nav_graph)
navController.graph = infoGraph
true
}
}
false
}