超过2片段之间的通信 [英] Communicating between more than 2 fragments

查看:170
本文介绍了超过2片段之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在它三个片段的活动,这是我的三个片段之间如何沟通:

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 FragmentB 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 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屋!

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