超过2片段之间的通信 [英] Communicating between more than 2 fragments
问题描述
我在它三个片段的活动,这是我的三个片段之间如何沟通:
I have an activity with three fragments in it, This is how I communicate between the three fragments:
首先,我仅显示碎裂
在 ActivityA
。基于一个 RadioButtonSelection
,我添加相应的 FragmentB
(两个布局可用)和 FragmentC
。如果输入 FragmentB
更改字符串
,那么 FragmentC的
的意见的变化,但如果单选
选择在碎裂
修改然后我删除片段
B和 FragmentC
并添加他们再次对应于输入从碎裂
。
First I display only FragmentA
in ActivityA
. Based on a RadioButtonSelection
, I add the corresponding FragmentB
(two layouts available) and a FragmentC
. If input in FragmentB
is changed String
, then FragmentC's
views change but if the RadioButton
choice in FragmentA
changes then I remove Fragment
B and FragmentC
and add them again corresponding to input from FragmentA
.
现在,我实现了在碎裂
一个沟通的界面,另一个在 FragmentB
。它看起来相当混乱。我如何能实现一个单一的界面,他们三人与活动之间的沟通?
Now, I've implemented a communicator interface in FragmentA
and another in FragmentB
. It looks quite messy. How can I implement a single interface to communicate between the three of them and the activity?
感谢。
推荐答案
在这种情况下,在多个片段和活动要与对方沟通,两个选项都记在心里。
In such circumstances in which multiple fragments and activities want to communicate with each other, two options come in mind.
- Java接口 - 只有有一个活动一或多个片段。每个片段应该实现自己的接口。活动也用于它们之间的共享信道。
推荐
的
- Java Interfaces - Only one activity and one or more fragments. Each fragment should implement its own interface. Activity is also used for a shared channel among them.
Recommended
<子>图片来源:<一个href=\"http://stackoverflow.com/a/24083101/1841194\">http://stackoverflow.com/a/24083101/1841194
- 本地广播 - 可以在多个活动和服务中使用。这也可用于片段之间的通信。
- Local broadcasts - Can be used among multiple activities and services. This also can be used for communicating between fragments.
这篇关于超过2片段之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!