fragment - '在导航返回'与后按钮的方式不同

发布于 2025-02-09 13:17:23 字数 686 浏览 0 评论 0原文

我正在编写Android应用程序,使用片段。

有两种主要方法可以向上或向后导航。

屏幕的图片

  1. 首先是在导航栏上使用箭头(标记为图片中的数字1)

  2. 第二个使用系统后按钮(标记为数字2)

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
...
        requireActivity().onBackPressedDispatcher.addCallback(this) {
                findNavController().navigate(R.id.action_updateFragment_to_listFragment)
...    
}

它可以正常工作,但仅适用于系统返回按钮(2)。

如何通过单击导航栏(1)的背面箭头引起的动作?

谢谢!!!

I'm writing android app, using fragments.

There are two main ways to navigate up or back.

picture of the screen

  1. The first is to use the arrow on the navigation bar (marked with the number 1 in the picture)

  2. The second use the system back button (marked with the number 2)

I wrote:

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
...
        requireActivity().onBackPressedDispatcher.addCallback(this) {
                findNavController().navigate(R.id.action_updateFragment_to_listFragment)
...    
}

and it works just fine, but only for system back button(2).

How can I override action caused by click on the back arrow from navigation bar(1)?

thanks!!!

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

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

发布评论

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

评论(1

伴随着你 2025-02-16 13:17:23

好吧,我知道了。

override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if(item.itemId == android.R.id.home) {
            Toast.makeText(requireContext(), "the answer", Toast.LENGTH_SHORT).show()
        }
        return super.onOptionsItemSelected(item)
    }

我希望这篇文章将来会帮助某人。

Ok, I've got this.

override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if(item.itemId == android.R.id.home) {
            Toast.makeText(requireContext(), "the answer", Toast.LENGTH_SHORT).show()
        }
        return super.onOptionsItemSelected(item)
    }

I hope this post will help someone in the future.

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