Pass bundle intent in android using MVP
Asked Answered
S

1

6

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?

Situated answered 31/1, 2017 at 7:36 Comment(0)
P
12

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.

Pika answered 31/1, 2017 at 20:2 Comment(2)
Okay, your answer does make a lot of sense. But how would you go about 3rd party libraries that require some sort of work to be done with the data received in a calling Activity's onActivityResult() method?Whity
Why would you persist "selected" item?! That doesn't make any sense. I mean it's not something you will see in the long term, you want your selection to be cleared as soon as you exit the DetailsActivity. A better solution is to keep cached items in the repository and save temporary view states (checked/clicked/selected and etc) there.Asare

© 2022 - 2024 — McMap. All rights reserved.