Communicating between more than 2 fragments

前端 未结 2 1892
天命终不由人
天命终不由人 2020-12-22 02:24

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<

相关标签:
2条回答
  • 2020-12-22 02:49

    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

    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.
    0 讨论(0)
  • 2020-12-22 02:56

    I would also recommend implementing an interface. Here is a good blog post guiding about Inter fragment communication. If you do not want to implement interface then you can use Eventbus. A good library for that would be Otto

    0 讨论(0)
提交回复
热议问题