Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing MVP on a single activity with two (or multiple) fragments

I'm developing a small application that shows a list, and when an item is clicked it opens a secondary screen with the item details. I want to implement MVP as my architecture for this app, and i have been struggling figuring out how to do that when I have a single activity with 2 fragments.

Some questions came up as when an item from the list is clicked, a callback is sent to the first presenter, but at this point, who is in charge of opening the second fragment? do the presenters 'talk' to each other? should i do it through the activity?

Been looking around for examples of single activity with multiple fragments implementing MVP, but couldn't find something like that yet.

(Yes, it can be done otherwise, but the purpose of the app is to learn implementing MVP on a single activity with multiple fragments)

Appreciate any help! Thanks!

like image 921
mgR Avatar asked Nov 07 '22 16:11

mgR


1 Answers

After looking into different existing MVP sample projects I've decided to follow most of the concepts in the 'TODO-MVP-TABLET' git project by Google which can be found here:

https://github.com/googlesamples/android-architecture/tree/dev-todo-mvp-tablet

I've chosen this solution due to the level of abstraction and the ability to later on reuse any fragment in other activities without changing any code.

Solution principles:

  • Each fragment has a presenter defined by an interface.
  • There is a bigger presenter implementing all the smaller presenters.
  • The bigger presenter holds references to all of the smaller presenters and when a method is invoked it simply delegates the action to the relevant presenter.
  • Fragments are given the 'big' presenter as their presenter without actually being aware this is the case.
  • Smaller presenters should hold the reference to the view.

Diagram taken from Google's github page:

solution diagram from the github project


Update: Link isn't valid, seems like Google removed the project from their samples. Will leave it in case they reupload it.

like image 180
mgR Avatar answered Nov 14 '22 22:11

mgR