Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
701 views
in Technique[技术] by (71.8m points)

android - Communicating between more than 2 fragments

I have an activity with three fragments in it, This is how I communicate between the three fragments:

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.

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?

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

enter image description here Image source: https://stackoverflow.com/a/24083101/1841194

  • Local broadcasts - Can be used among multiple activities and services. This also can be used for communicating between fragments.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...