如何解决在Jetpack导航中处理后堆栈
我在Jetpack Navigation中使用BottomNavigationView
。我还将所有片段都设置为顶级目的地。
val host : NavHostFragment = supportFragmentManager
.findFragmentById(R.id.nav_host_fragment) as NavHostFragment? ?: return
val navController = host.navController
val appBarConfig = AppBarConfiguration(topLevelDestinationIds = setOf(R.id.library_fragment,R.id.account_fragment,R.id.calender_fragment,R.id.home_fragment,R.id.notice_fragment))
val bottomNavView = binding.bottomNavView
setSupportActionBar(binding.toolbar)
setupActionBarWithNavController(navController,appBarConfig)
bottomNavView.setupWithNavController(navController)
在第二个底部导航菜单项上,我正在使用LibraryFragment
的{{1}}。 TabLayout
中有一个LibraryFoldersFragment
对应于一个Tab。 LibraryFragment
包含一个LibraryFoldersFragment
。点击RecyclerView
项后,RecyclerView
将替换LibraryFilesFragment
。
LibraryFoldersFragment
问题在于,在val fragment = LibraryFilesFragment.newInstance(item)
childFragmentManager.beginTransaction().addToBackStack(null).replace(R.id.frameLayout,fragment)
.commit()
应用中按返回按钮时,直接导航到第一个底部导航菜单项。但我希望它导航到LibraryFilesFragment
。
解决方法
由于您使用的是Jetpack的Navigation组件,而不是自己提交碎片事务。创建一个导航XML,以声明您的LibraryFoldersFragment
至LibraryFilesFragment
导航。在recyclerView项目单击上使用findNavController().navigate()
进行导航。
设置带有NavController的bottomNavView,并创建具有不同所需导航XML的GraphId。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。