Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass bundle intent in android using MVP

I want to pass the Model data into another activity using Parceler through Bundle intent. My problem is how could I pass the data from Presenter into the View layer to display in another activity using MVP architecture in android?

like image 300
vidalbenjoe Avatar asked Jan 31 '17 07:01

vidalbenjoe


1 Answers

This is certainly possible. Presuming that your Activity implements your View interface you'd have a method in the interface like:

void startNextActivity(MyData data);

Then in the Activity:

@Override
void startNextActivity(MyData data) {

    // create bundle
    // send intent
}

And in Presenter:

view().startNextActivity(myData);

However I don't recommend that you do this

I'm of the opinion that quite a few classic Android patterns should be used sparingly when doing MVP. This includes things such as onActivityResult & passing data around between Activities/Fragments using Bundle.

To keep things as decoupled and clean as possible Activities should avoid talking to other Activities, Presenters shouldn't talk to other Presenters, etc. If you need to access data from one Activity in another Activity then send it to the model to be persisted. The next Activity will then be sent this data by its Presenter which will have got it from the model.

The following diagram gives a better overview:

MVP Diagram

Rather than passing the details as part of the Bundle when starting the next Activity they are persisted in the model for the next Activity to load.

like image 130
Jahnold Avatar answered Oct 26 '22 23:10

Jahnold