Android:覆盖在 Fragment 中定义的应用栏中的向上导航 [英] Android: Override Navigate Up in App Bar Defined in Fragment

查看:28
本文介绍了Android:覆盖在 Fragment 中定义的应用栏中的向上导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用栏从我的片段定义而不是使用活动

I have an app bar defined from my fragment rather than activity by using

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

        binding.toolbar.apply {
            //add menu
            inflateMenu(R.menu.menu_fragment)

            //setup with navcontroller/navgraph
            setupWithNavController(findNavController())
        }
}

我面临的问题是,当用户使用应用栏单击向上导航"按钮时,尝试实施警告消息.我只想要一个片段中的这种行为.

The problem I'm facing is trying to implement a warning message when a user clicks the Navigate Up button using the app bar. I want this behaviour only in one fragment.

我在网上找到了与活动中定义的应用栏有关的解决方案,但它们似乎对我不起作用(例如使用覆盖 fun onSupportNavigateUp().如果我能做到这一点,有什么想法吗?

I've found solutions online pertaining to app bars defined in an activity but they don't seem to work for me (such as using override fun onSupportNavigateUp(). Any ideas if I may be able to accomplish this?

更新

最初,我实现了所选择的答案,该答案有效但导致了一些内存泄漏.回答这个问题的好心人还找到了解决内存泄漏的方法此处.不幸的是,它对我来说效果不佳(我相信是因为我使用的是导航组件)但它可能对您有用.

Initially, I implemented the chosen answer which worked but was causing some memory leaks. The kind individual who answered this question also found a workaround for the memory leaks here . Unfortunately, it didn't work so great for me (I believe because I am using navigation components) but it may work for you.

我后来意识到,通过将这行代码添加到我的工具栏代码中,我可以轻松地覆盖向上导航的默认行为:

I later realized that I could easily override the navigate up default behaviour by adding this piece of line to my toolbar code:

        binding.toolbar.apply {
            //add menu
            inflateMenu(R.menu.menu_fragment)

            //setup with navcontroller/navgraph
            setupWithNavController(findNavController())
            
            //****************ADD THIS******************
            setNavigationOnClickListener { view ->
                //do what you want after user clicks navigate up button
            }
        }

推荐答案

我面临的问题是,当用户使用应用栏单击向上导航"按钮时,尝试实施警告消息.我只想要一个片段中的这种行为.

The problem I'm facing is trying to implement a warning message when a user clicks the Navigate Up button using the app bar. I want this behaviour only in one fragment.

因此,您只需要捕捉点击该特定片段的应用栏的 UP 按钮的事件.

So, you just need to catch the event of hitting the UP button of the app bar for that particular fragment.

您可以为该片段启用选项菜单:

You can enable the options menu for that fragment:

setHasOptionsMenu(true)

并覆盖 onOptionsItemSelected 以捕获 UP 按钮 ID:

And override onOptionsItemSelected to catch the UP button id:

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    if (item.itemId == android.R.id.home) {
        // Handle the UP button here
        Toast.makeText(requireContext(), "UP button clicked", Toast.LENGTH_SHORT).show()
        return true
    }
    return super.onOptionsItemSelected(item)
}

注意:如果你想为那个片段使用一个独特的工具栏而不是默认的工具栏,检查这个答案.

Note: if you want to use a unique toolbar for that fragment other than the default one, check this answer.

现在我无法使用 inflateMenu(R.menu.menu_fragment) 来扩充我的菜单.有什么想法吗?

now I am unable to inflate my menu using inflateMenu(R.menu.menu_fragment). Any ideas?

你可以移除这个通胀,而是重写onCreateOptionsMenu:

You can remove this inflation, and instead override onCreateOptionsMenu for that:

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
    inflater.inflate(R.menu.menu_fragment, menu)
}

这篇关于Android:覆盖在 Fragment 中定义的应用栏中的向上导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆