Communicate between leading in MVP android application

I use the MVP pattern to create a small test application for Android. I have two fragments of Fragment B (I use for a sliding drawer) and fragment A (main fragment). Both fragments have their own leaders. when I click on a sliding draw, he should send a message or call a method in fragment A to update the view. I want to ask how both fragment presenters can talk under MVP. I know other solutions, but I want to do this through the MVP pattern.

Please suggest some options that follow the MVP pattern to solve such scenarios.

+7
android mvp android-fragments
source share
2 answers

First of all, in MVP approaches, the presenter and presentation are 1 to 1 related to each other. If you want to communicate between presenters using a bus system such as EventBus / RxBus.

I recommend the following tutorial. This is a 5 part tutorial. This tutorial has two fragments (search fragments and cache memory) that interact with each other.

https://hackernoon.com/yet-another-mvp-article-part-1-lets-get-to-know-the-project-d3fd553b3e21

0
source share

In MVP, the View has Context to start another view, which is either a different Fragment or Activity , so any transition between your Fragments must be through the View. In your case, you have:

View1 (drawer fragment) <- β†’ Presenter1

View2 (main snippet) <- β†’ Presenter2

You click on the widget on View1 and want to go to some screen on View2 using MVP. You can do it like this:

---------------------- View 1 ---------------------

 view1Item.setOnClickListener(new OnClickListener({ presenter1.doWhenItem1IsClicked(); })) 

---------------------- Presenter 1 ----------------

 public void doWhenItem1IsClicked(){ mView.showRelevantPageOnMainScreen() } 

---------------------- View 1 ---------------------

 public void showRelevantPageOnMainScreen(){ View2 view2=new View2(); //This is better to be done using DI getFragmentManager().beginTransaction().replace(R.id.your_main_page_layout,view2).commit(); } 

---------------------- View 2 ---------------------

 public void onCreate(){ super.onCreate(); presenter2=new Presenter2(this); } . . . 

I wrote the MVP library here, you may find it useful.

0
source share

All Articles