0

The Android Developer Documentation states that one should avoid direct communication between fragments by implementing an interface. On further searching, the reason that I found for this is that it causes tight-coupling between the fragments. I'm having some difficulty understanding why it causes tight coupling and why that might be a problem.

Could someone show,

by example code:

  1. How direct communication between fragments would work.
  2. Why this approach would be problematic
  3. How using interfaces would solve the aforementioned problem.
xasthor
  • 119
  • 3
  • "The Android Developer Documentation states that one should avoid direct communication between fragments by implementing an interface" -- do you have a link to this? – CommonsWare Apr 13 '16 at 16:15
  • "All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly." http://developer.android.com/training/basics/fragments/communicating.html – xasthor Apr 13 '16 at 16:17
  • That does not match what you have in your question. – CommonsWare Apr 13 '16 at 16:19
  • 1
    Essentially the issue is that Fragments should be completely self-contained. They are meant to be modular and re-usable. So if you have an Activity, and you want to use a certain Fragment, you shouldn't have to also add another Fragment in order to use the first Fragment. That's what's supposed to be avoided here. Using an interface makes it so the Fragment just sends back data to the parent Activity, and the Activity is responsible for doing something with it. The Activity could then process and distribute that data to another Fragment, but the Fragments shouldn't talk to one another. – NoChinDeluxe Apr 13 '16 at 16:25
  • 1
    Basically what you're supposed to do is use the host activity as a broker for messaging between the fragments, kind of like an event bus. The fragments don't need to know specifically about each other. They just need to know what events to publish for the activity to propagate to the relevant fragments it contains. This level of indirection prevent the fragments from having to deep dive into each others' implementations, which would be fragile. – Doug Stevenson Apr 13 '16 at 16:26
  • @NoChinDeluxe Yeah, but the problem is that I'm not able to understand **why** direct communication between two fragments would affect their reusability. Which is why I'm asking for code examples so I can better understand this concept. – xasthor Apr 13 '16 at 16:39
  • see the question http://stackoverflow.com/q/6751583/794088 and its top two answers. – petey Apr 13 '16 at 16:53
  • @xasthor -- Well I just explained it in my comment above. Imagine if you have 3 Fragments, A, B, and C. They all talk directly to each other. Now it's impossible to just use A by itself, because it needs communication from B and C. But what if you don't need B and C? Now you're forced to use them as well because A doesn't work by itself. That defeats the whole purpose of using Fragments in the first place. If A had just talked to your Activity instead, your Activity can decide what to do with that data, such as sending it to an OPTIONAL B or C Fragment. – NoChinDeluxe Apr 13 '16 at 16:55
  • and http://stackoverflow.com/q/29328171/794088 – petey Apr 13 '16 at 16:55

1 Answers1

0

Android fragments are often thought of as Views in MVP or MVC patterns, they should not contain any application logic within. All meaningful events from the fragment should be delegated to presenters which are often implemented via Activity.

Suppose you have a fragment A and a fragment B. Fragment A displays a list of items and fragment B displays details for the selected item. If you communicated fragments directly it would create a tight coupling between them since fragment A would need a concrete fragment B reference in order to instantiate it. If your application requirements change, for instance by showing fragment C instead of B, the tight coupling will pop out and you would have to deal with it. You can avoid this coupling by introducing a Presenter or Controller interface into your fragment. By calling this interface methods you can be sure that the implementation of your presenter logic is decoupled from fragment appearance logic.

For more information about developing decoupled application architecture check out this article http://fernandocejas.com/2014/09/03/architecting-android-the-clean-way/

satorikomeiji
  • 469
  • 6
  • 16