1

I'm refactoring an android component to be adopting MVP architectural pattern. The problem I faced is that I have a fragment nesting other fragments in it.

1- The parent fragment hides/shows one of the nested fragments based on some conditions.

2- The child fragment passes data to the parent fragment which is observing it as here inspired by callback mechanism between fragment and activity.

I've 2 questions:

1- If I consider the fragment as the view of MVP, should I use distinct presenters for the parent fragment & the child fragment (1-to-1 mapping between presenters & views) or only one presenter for both and why?

2- If I'm supposed to use distinct presenters, how should I handle passing data from the child fragment to the parent fragment as I barely know the Cons. and Pros. of:

  • Using an EventBus framework like Otto
  • Allow a presenter to have a direct reference on another presenter
  • Keep the communication in the view layer, away from the presenters as here, by having the nested view delegates calls it receives from its presenter to the parent view.
Islam Salah
  • 953
  • 11
  • 22
  • I think it is better to separate these nested fragments, I haven't seen nested fragments as best practice. Put each fragment in a separate file. – salmanseifian Jun 20 '18 at 06:18

2 Answers2

2

As with most architectur questions, I honestly think there is no right or wrong way. So please treat this just a suggestion (how I would implement this)

Each MVP unit should contain it's own presenter, which means there is one parent presenter (for the parent fragment) and several child presenters (one for each child fragment).

The child presenters all contain a parentPresenter field, which acts as a way to pass data / messages from the child to the parent. This parentPresenter is NOT the real presenter object, but an interface that includes only the needed calls.

If you need to pass data / messages the other way around (from the parent to the children), this is implemented via interface methodes in the view:

  • the parentPresenter calls its view
  • the parentView finds it's childFragment
  • the childFragment calls the appropriate interface call on the childPresenter

This way the whole communication is hidden behind clean interfaces and is also nicely testable. Hope this helps and let me know if you have any questions...

Bmuig
  • 1,059
  • 7
  • 12
  • I highly appreciate: - your point of view in handling architecture questions. - your very organized answer that almost no one needs to read it twice to get your point of view. I agree with the point of having distinct presenter for each view. Regarding the second point, could you explain what is the difference between applying callback mechanism between views and applying it between presenters? Also any android sample supporting your answer will be highly appreciated. – Islam Salah Jun 20 '18 at 13:34
  • e.g samples: [Todo app](https://github.com/googlesamples/android-architecture/tree/todo-mvp/)-[Bourbon](https://github.com/hitherejoe/Bourbon)-[Mindorks](https://github.com/MindorksOpenSource/android-mvp-architecture) – Islam Salah Jun 20 '18 at 13:35
1

What I do in my application is using a callback mechanism between parent and child fragments for passing data between. I also made a different presenters for each child fragment, because if one day I would like to use only one of the child fragments I would only override it's presenter methods.

Zach Bublil
  • 857
  • 10
  • 28