简体   繁体   中英

MVP Android: how to pass data from one activity to another?

I have the following doubt and I'm wondering if this breaks or not the MVP pattern.

My project has Activity1 where an user can fill a form and then the presenter, related to that activity, on a button click retrieves data from a database based on the information given by the user and put it into a list.

Activity2 has the job to display the objects of this list.

Is it ok (according to MVP) that Activity1 put the list into an Intent and then start Activity2 with that Intent? (I don't think so).

Should the presenter of the Activity1 pass the list to the presenter of the Activity2? But how can this be done if presenters can't communicate each other?

Personally I use a single Activity with Fragments which makes it easy to pass data around. For inter-activity communication, I just use Intents as you have suggested.

If you're using Kotlin you should check Navigation Component related topics.

Navigation component is the a best practice when navigation between Activities/Fragments and allows you to pass data as arguments between them.

On the other hand you can pass those arguments in your intent view Bundle and retrieve them in the second activity, separating the logic from the view layer, and only calling a method to present the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM