6

I have an Activity A hosting two main Fragment F1 and F2. Both F1 and F2 have nested Fragment, each with its own Listener interface for exchanging data.

From what I understood from the answer to this question, the activity A:

  • needs to know every single interface declared by the fragments hosted by F1 and F2

  • needs to route the events generated by the fragments in F1 and F2 to the correct main fragment, F1 or F2.

If I understood correctly, there is no modularity in this approach: the activity needs to know everything about both the fragments it hosts (F1 and F2) and the fragments that are nested in F1 and F2.

Am I correct? For sure, I am very confused ...

Community
  • 1
  • 1
Antonio Sesto
  • 2,868
  • 5
  • 33
  • 51
  • Your nested fragments can talk directly to their parent fragments using `getParentFragment()` if that is what you ask? Thus no need to have nested child fragments talk to Activity in all cases. If you need more freedom of how your Activities/Fragments communicate, you can use EventBus https://github.com/greenrobot/EventBus, it essentially allows you to broadcast objects across your app. I use it to e.g. say 'update ui' whenever db values has changed. Then whichever Activities/Fragments are alive and listening will then update the ui accordingly. Seems lighter than setting up callbacks – cYrixmorten Mar 29 '15 at 11:53

1 Answers1

6

If you look at the Communicating with Other Fragments tutorial, it says that:

All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.

The reason for this is that Fragments are fluid & dynamic UI components that may fade in and out of view. Only the hosting Activity is capable of determining if a Fragment is added to the UI or has been detached from it.

If I understood correctly, there is no modularity in this approach: the activity needs to know everything about both the fragments it hosts (F1 and F2) and the fragments that are nested in F1 and F2.

The Fragments are "modular" in the sense that they are totally independent & reusable UI blocks. Also, they are "modular" because their interfaces are well defined and explicitly implemented by the hosting Activity. Anywhere you drop that Fragment in, if the Activity implements the callback interface defined in the Fragment, then the Activity "chooses" what to do depending on whether the Fragment is added / attached to the UI or not.

If we loosely apply the MVC way of thinking here, the hosting Activity acts as a controller of sorts between two views which are the Fragments. Of course this is just a loose analogy, but hopefully you get the picture.

Further considerations:

There is an alternative to this approach: a method called getParentFragment() that a nested Fragment can use to get a reference to the "outside" Fragment.

References:

1. Why direct communication between fragments is not recommended?.

2. Communicating with other fragments or to activity.

3. Fragment question: inter-fragment communication?.

Community
  • 1
  • 1
Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • 4
    Thank you, that's how I understood. It's not very elegant: if the activity has one fragment (or more) hosting 100 fragments, the activity needs to implement 100 different interfaces, when these interfaces are just for communicating between the main fragments and its children. – Antonio Sesto Mar 29 '15 at 15:26