Android 和 Fragment 结果 API。如何共享具有不同请求密钥的子片段的结果?

发布于 2025-01-10 09:57:12 字数 1371 浏览 0 评论 0原文

我正在尝试在我的应用程序中实现片段结果 API。我需要在片段内显示 DialogFragment,并在单击对话框按钮时将结果从对话框片段发送到片段。 因此,在我的对话框片段中,当单击按钮时,我做了如下操作:

  setFragmentResult(requestKey, bundleOf(RESULT to result))

我正在尝试在 onCreate() 内检索我的片段中的结果:

  childFragmentManager.setFragmentResultListener(requestKey, this) { _, bundle ->
        val result = bundle.getInt(DialogFragment.RESULT)
       // some code
    }

一切正常,但有一个问题。我在对话框片段中调用 setFragmentResult 时使用的 requestKey 是动态的。

也就是说,在不同情况下可以在片段内部打开对话框。我需要区分这些情况。为此,我向对话框片段发送不同的请求密钥。 但是,当我收到片段中的结果时,如何通过键区分该结果。如果我在 onCreate() 方法中执行此操作。

也就是说,我想做这样的事情:

 childFragmentManager.setFragmentResultListener(requestKey, this) { requestKey, bundle ->
            when(requestKey) {
                "FIRST_CASE" -> { // some code }
                "SECOND_CASE" -> { // some code }  
          }
        }

但我不明白如果这个方法(setFragmentResultListener)已经需要一个requestKey作为参数,那么如何实现它。我应该从哪里拿它?

如果我的 requestKey 是静态的,我会在对话框片段中创建一个常量并使用它。但我的 requestKey 是动态的。

请帮我。我在互联网上没有找到适合我的例子。目前我不明白如何在 MVVM 的干净架构环境中实现这一点。

PS 这是我想要实现的示例: 包含两个按钮的片段(例如AB)。通过单击每个按钮,将打开带有不同文本的对话框片段。对话框内还有一个按钮,单击该按钮即可将结果传输到片段。我需要以不同的方式处理这个结果,具体取决于片段上按下的按钮(A 或 B)。为此,我想通过请求密钥来区分。

I am trying to implement fragment result API in my application. I need to show DialogFragment inside Fragment and send result from dialog fragment to fragment when dialog buttons was clicked.
So in my dialog fragment when button was clicked I making smth like this:

  setFragmentResult(requestKey, bundleOf(RESULT to result))

And I'am trying to retrieve result in my fragment inside onCreate():

  childFragmentManager.setFragmentResultListener(requestKey, this) { _, bundle ->
        val result = bundle.getInt(DialogFragment.RESULT)
       // some code
    }

Everything works well, but there is one problem. The requestKey I use when calling the setFragmentResult in my dialog fragment is dynamic.

That is, the dialog can be opened inside the fragment in different cases. And I need to distinguish between these cases. To do this, I send a different request key to the dialog fragment.
But how do I, when receiving a result in a fragment, distinguish this result by key. If I do this inside the onCreate() method.

That is, I want to do something like this:

 childFragmentManager.setFragmentResultListener(requestKey, this) { requestKey, bundle ->
            when(requestKey) {
                "FIRST_CASE" -> { // some code }
                "SECOND_CASE" -> { // some code }  
          }
        }

But I do not understand how this can be implemented if this method (setFragmentResultListener) already requires a requestKey as an argument. Where should I take it from?

If my requestKey was static I would create a constant inside the dialog fragment and use it. But my requestKey is dynamic.

Please help me. I did not find a suitable example for me on the Internet. And at the moment I don’t understand how this can be implemented in the context of a clean architecture with MVVM.

P.S. Here is an example of what I want to implement:
A fragment that contains two buttons (e.g. A, B). By clicking on each of the buttons, dialog fragment opens, with different text. Inside the dialog there is also a button, by clicking on which the result is transferred to the fragment. I need to process this result differently, depending on which button was pressed on the fragment (A or B). To do this, I wanted to distinguish by request key.

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

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

发布评论

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

评论(1

醉态萌生 2025-01-17 09:57:13

FragmentResultListener 收到 requestKey 的原因是,如果您在多次调用 中重复使用相同的 FragmentResultListener setFragmentResultListener - 即,如果您的 Fragment 本身实现了 FragmentResultListener

在您的情况下,您的 Fragment 没有实现 FragmentResultListener,因此您只需调用 setFragmentResultListener 两次,每个键一次:

childFragmentManager.setFragmentResultListener("FIRST_CASE", this) { _, bundle ->
  // some code
}
childFragmentManager.setFragmentResultListener("SECOND_CASE", this) { _, bundle ->
  // some code
}

The reason the FragmentResultListener received a requestKey is if you are reusing the same FragmentResultListener in multiple calls to setFragmentResultListener - i.e., if your Fragment itself implemented FragmentResultListener.

In your case, your Fragment isn't implementing the FragmentResultListener, so you can just call setFragmentResultListener twice, once for each key:

childFragmentManager.setFragmentResultListener("FIRST_CASE", this) { _, bundle ->
  // some code
}
childFragmentManager.setFragmentResultListener("SECOND_CASE", this) { _, bundle ->
  // some code
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文