当我使用 optionsMenu 导航特定片段时,为什么不能导航回来?

发布于 2025-01-13 05:42:42 字数 2140 浏览 1 评论 0原文

我使用导航组件最新的稳定版本。

我想在我的应用程序中创建一个快捷选项菜单项,它导航到一个片段。

该片段也由抽屉菜单使用。

因此,当我使用抽屉菜单进行导航时,一切都很好,但是当我导航到例如片段 B 时,之后我使用选项菜单导航到片段 C,之后我无法从抽屉导航回片段 B ,因为它总是显示Fragment C。

我可以通过在导航到Fragment C之前添加navController.popBackStack()来解决这个问题,但是这根本不好,因为之前的fragment被破坏了,我无法返回到按下后退按钮即可。

这个问题有什么解决办法吗?

MainActivity
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_main,menu)

        menu?.findItem(R.id.action_worksheet_management)?.setOnMenuItemClickListener {
            val navController = findNavController(R.id.nav_host_fragment_content_main)

            navController.popBackStack()

            navController.navigate(R.id.nav_work)

            return@setOnMenuItemClickListener true
        }

        return true
    }

private fun setUpNavigation() {
        val navController = findNavController(R.id.nav_host_fragment_content_main)
        setSupportActionBar(binding.appBarMain.toolbar)

        val menuItems = mutableListOf(
            R.id.nav_worksheet_browser,
            R.id.nav_product,
            R.id.nav_about,
            R.id.nav_company_info,
            R.id.nav_services,
            R.id.nav_stock_receive,
            R.id.nav_synchronization,
            R.id.nav_order_return,
            R.id.nav_work,
            R.id.nav_settings,
            R.id.nav_certificates
        )

        appBarConfiguration = AppBarConfiguration(
            menuItems.toSet(),
            binding.drawerLayout
        )

        setupActionBarWithNavController(navController, appBarConfiguration)
        binding.navView.setupWithNavController(navController)
    }

  override fun onBackPressed() {
        if (binding.drawerLayout.isDrawerOpen(GravityCompat.START)) {
            binding.drawerLayout.closeDrawer(GravityCompat.START)
        } else {
            super.onBackPressed()
        }
    }

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.nav_host_fragment_content_main)
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }

I use Navigatation component latest stable version.

I would like to create a shortcut option menu item in my application, which is navigates to a fragment.

This fragment is also used by the drawer menu.

So when I use the drawer menu for navigation, everything is good, but when I navigate to for example Fragment B and after that I use the options menu to navigate to Fragment C, and after the I can not navigate back to Fragment B from drawer, because it always shows Fragment C.

I can solve this problem by adding navController.popBackStack() before the navigation to Fragment C, but this is not good at all, becaouse the previous fragment is destroyed, and I can not go back to it, by pressing back button.

Is there any solution of this problem?

MainActivity
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_main,menu)

        menu?.findItem(R.id.action_worksheet_management)?.setOnMenuItemClickListener {
            val navController = findNavController(R.id.nav_host_fragment_content_main)

            navController.popBackStack()

            navController.navigate(R.id.nav_work)

            return@setOnMenuItemClickListener true
        }

        return true
    }

private fun setUpNavigation() {
        val navController = findNavController(R.id.nav_host_fragment_content_main)
        setSupportActionBar(binding.appBarMain.toolbar)

        val menuItems = mutableListOf(
            R.id.nav_worksheet_browser,
            R.id.nav_product,
            R.id.nav_about,
            R.id.nav_company_info,
            R.id.nav_services,
            R.id.nav_stock_receive,
            R.id.nav_synchronization,
            R.id.nav_order_return,
            R.id.nav_work,
            R.id.nav_settings,
            R.id.nav_certificates
        )

        appBarConfiguration = AppBarConfiguration(
            menuItems.toSet(),
            binding.drawerLayout
        )

        setupActionBarWithNavController(navController, appBarConfiguration)
        binding.navView.setupWithNavController(navController)
    }

  override fun onBackPressed() {
        if (binding.drawerLayout.isDrawerOpen(GravityCompat.START)) {
            binding.drawerLayout.closeDrawer(GravityCompat.START)
        } else {
            super.onBackPressed()
        }
    }

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.nav_host_fragment_content_main)
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青瓷清茶倾城歌 2025-01-20 05:42:42

我通过将导航版本从 2.4.1 更改为 2.3.5 解决了这个问题。我希望稍后能修复它。

I resolved it, by changed the nav version from 2.4.1 to 2.3.5. I hope it will be fixed later.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文