Newaj
Newaj

Reputation: 4450

How to pass data from one activity to another using MVP pattern?

I have a Recyclerview in MainActivity where data are from an API. I want to open a DetailsActivity after clicking an item.

Now in MVP pattern, how should I pass the data object from MainActivity to DetailsActivity? I used Interactor in Mainactivity to handle data part.

Upvotes: 1

Views: 539

Answers (2)

Onik
Onik

Reputation: 19949

Under an assumption DetailsActivity is supposed to show "details" that have id, you may pass the id through a Bundle to DetailsActivity and fetch "details" by the id.

If the assumption is wrong, then you might make the "details" Parcelable and pass them through a Bundle to the DetailsActivity.

By using either of the approaches it is guaranteed that the data passed through a Bundle will "survive" a process death in case your app process is killed by the system in background. I.e., when navigating back to the app the Bundle will be "redelivered" to DetailsActivity.

I can directly pass the data to the DetailActivity through Intent, but how is the MVP approach here?

In MVP, a view (V) is usually platform specific, so it's fine for it (in Android) to operate with Bundle.

That means I can directly send data to DetailActivity?

Yes, it might be as follows. Presenter (P) of DetailsActivity gets an id passed via Intent and "asks" Interactor to get the details data from Repository (or some other abstraction you use).

Upvotes: 1

milad salimi
milad salimi

Reputation: 1660

I think you should use callback in your adapter and pass data from your adapter to

your first activity then pass data from first activity to second activity with Parcelable.

You can follow this for use Parcelable.

[https://developer.android.com/reference/android/os/Parcelable

Upvotes: 1

Related Questions