如何使用具有有效后退堆栈的 BottomNavigationView 显示片段?
How can I show a fragment using a BottomNavigationView with valid back stack?
编辑:
原问题是Is there a way to get a reference to a fragment that is displayed using a BottomNavigationView?
。但我已经想通了一些事情并意识到我问错了问题。
我想获取对正在使用 BottomNavigationView
显示的片段的引用。
这就是我的 BottomNavigationView
的设置方式。它在 Activity 的 onCreate
中。
val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.bottom_nav_view_nav_host)
val appBarConfiguration = AppBarConfiguration(setOf(
R.id.navigation_first_list,
R.id.navigation_second
))
setupActionBarWithNavController(navController, appBarConfiguration)
bottomNav.setupWithNavController(navController)
我试图通过 bottomNav.findFragment<TheFragmentType>()
获取片段,但它抛出异常。
原来我问错了问题。我可以只使用 navController 调用正确的导航组件,以便显示带有返回堆栈的正确片段。
val bundle = bundleOf("someId" to "theId")
navController.navigate(R.id.action_navigation_list_to_details, bundle)
编辑:
原问题是Is there a way to get a reference to a fragment that is displayed using a BottomNavigationView?
。但我已经想通了一些事情并意识到我问错了问题。
我想获取对正在使用 BottomNavigationView
显示的片段的引用。
这就是我的 BottomNavigationView
的设置方式。它在 Activity 的 onCreate
中。
val navView: BottomNavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.bottom_nav_view_nav_host)
val appBarConfiguration = AppBarConfiguration(setOf(
R.id.navigation_first_list,
R.id.navigation_second
))
setupActionBarWithNavController(navController, appBarConfiguration)
bottomNav.setupWithNavController(navController)
我试图通过 bottomNav.findFragment<TheFragmentType>()
获取片段,但它抛出异常。
原来我问错了问题。我可以只使用 navController 调用正确的导航组件,以便显示带有返回堆栈的正确片段。
val bundle = bundleOf("someId" to "theId")
navController.navigate(R.id.action_navigation_list_to_details, bundle)