Inter-Fragment Communication

6 thoughts on “Inter-Fragment Communication

  1. Hi,
    thanks for your great posts, they are very clear.

    As you mentionned, the also EventBus is a good pattern to use.
    Actually, I would like to underscore that using the EventBus is very clean.
    You can use the EventBus for most inter-fragments communication but also Service to fragments, Broadcast to fragments, web service result to fragment, db to fragment, etc.
    And sometimes, even vice-versa.

    Keep in mind that a fragment should not know about other fragments.

  2. Thanks a lot. Is it possible to elaborate on the merits/demerits of having an explicit EventBus (as in Otto & Greenrobot’s versions). Again, thanks a lot.

  3. Yes, elaboration is very much possible. I’m planning to write a post on EventBus soon.

  4. Yes, indeed. EventBus does the pure decoupling of components. GreenRobot’s EventBus is the best among EventBus implementations for Android I’ve seen so far.

  5. Excellent article! Just one question:

    “Fragment A can keep a reference to a ‘Communicator’ instance and delegate the task to implementer of the interface.”
    If you keep this references, it’s not the same as to keep the Activity reference?
    Sorry for the basic question but I would like to understand this as I was always concerned about memory leaks.

    Thanks in advance!

  6. The use of Interface here is not dealing with memory leaks. Handling memory leaks is a different world. Interface here is saving a piece of code from tight coupling. Although we are passing reference of an Activity that implements Communicator, the Fragment that uses this communicator interface doesn’t need to know which specific class is implementing it. In other words, FragmentA has no idea about the existence of TheActivity. If you want to dig deeper into this, please check out my other blog post here

Leave a Reply

Your email address will not be published. Required fields are marked *