5

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!

mgR
  • 195
  • 1
  • 4
  • 11

2 Answers2

1

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.

mgR
  • 195
  • 1
  • 4
  • 11
  • 1
    The link you provided is not working... How many presenter implementations do you have? I have one activity with a presenter, now added 3 fragments. Activity presenter can implement fragment presenters, but how to deal with the fragment views? – Andrey Rankov Feb 12 '19 at 22:45
0

There are possibly many ways to implement MVP. Majorly we use 3 things. - View - Presenter - Modal

you should be clear with your working of screen before creating these things.

eg if you want a login screen. create structure (using interface) of activity first. like what your presenter and view will contain eg.

public interface LoginPresenter {
    void validateCredentials(String username, String password);
    void onDestroy();
}

View structure:

public interface LoginView {
void showProgress();

void hideProgress();

void setUsernameError();

void setPasswordError();

void navigateToHome();
}

Now these are the classes you need to implement on your view class (Activity/fragment) and presenter where your logic part resides.

Now about your queries.

which means the activity will have both presenters instances. No, your activity should not require to have multiple presenter. it already has opened fragment reference (by findfragmentby id or tag).

who is in charge of opening the second fragment?

you can open it from any of them either Activity/fragment. if Activity use getfragmentsupportManager if fragment use getfragmentManager

NOTE: For more info follow this git example. https://github.com/antoniolg/androidmvp

Harminder Singh
  • 316
  • 1
  • 11
  • Thanks for the answer, most of the MVP intro I already have and know (regarding the interfaces, etc).. you reffered to a git example but I can't see any link.. perhaps you forgot to add it? , And regarding to the activity not supposed to have multiple presenters.... well, if each fragment has a presenter, and the activity is the one instantiating them (seen this on a few projects, also on google's linked above in my post) , how would the activity not have multiple presenters? – mgR Sep 26 '17 at 19:45
  • Activity is the one who instantiating the fragments eg if you have login fragment as your first fragment, activity will instantiate it. All the workflow of login will be handled by your fragment's view, modal and presenter. when succesfully login first fragment will open other one. In the meanwhile you have common variables for both fragment you can place it over activity so that both fragment can access that. ((MainActivity)getActivity()).commonVariable – Harminder Singh Sep 27 '17 at 03:22