Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I pass data (e.g. which item was clicked) between Activities in MVP?

I have list of items shown with MVP pattern. View is an Activity (a.k.a. ItemsList). I want to show next Activity (also MVP'ed, a.k.a. ItemDetails) with details of item clicked in ItemsList.

Model for ItemsList is fed with data from Repository. So actually it's RMVP. It looks like this:

            |->Model<->Presenter<->View [ItemsList]
Repository<-|
            |->Model<->Presenter<->View [ItemDetails]

So Model for ItemsList already knows exactly what Model item was clicked so I could pass it right away from ItemsList to ItemDetails without fetching data from Repository again / mapping from data to Model / making new Model for ItemDetails etc.

How should I pass data (e.g. which item was clicked) between Activities in MVP?

Solution 1

Pass those data with Intent (similar to what is discussed here) but ... then what to do with that data? You unpack it from Intent in Activity (View) while you should have it on the other side of MVP, i.e. in Model. You pass it from View thru Presenter to Model?

But then Model in ItemDetails is not created from the "downside of MVP" (from Repository) but from "upperside of MVP" (from Presenter).

Solution 2

Pass only ID of clicked item to ItemDetails View (similar to what android10/Android-CleanArchitecture proposes in UserDetailsActivity with field private int userId; this is also what googlecodelabs' NoteDetailPresenter uses)

However there may a problem because I may have two implementations of Repository:

  • one with cache and then I may pass ID of clicked item to ItemDetails View (however it seems to be over-engineered), similar to what android10/Android-CleanArchitecture proposes in UserDetailsActivity with field private int userId;
  • second without cache and then I can't even ask for ID because I don't have access to list fetched for ItemsList
like image 593
Marian Paździoch Avatar asked May 11 '16 12:05

Marian Paździoch


People also ask

How do you store and pass data between activities in android?

The easiest way to do this would be to pass the session id to the signout activity in the Intent you're using to start the activity: Intent intent = new Intent(getBaseContext(), SignoutActivity. class); intent. putExtra("EXTRA_SESSION_ID", sessionId); startActivity(intent);

What is MVP pattern in Android?

MVP (Model — View — Presenter) architecture is one of the most popular architecture patterns and is valid in organizing the project. MVP (Model — View — Presenter) comes into the picture as an alternative to the traditional MVC (Model — View — Controller) architecture pattern.

What is presenter in Android?

A Presenter is used to generate View s and bind Objects to them on demand. It is closely related to the concept of an RecyclerView. Adapter, but is not position-based. The leanback framework implements the adapter concept using ObjectAdapter which refers to a Presenter (or PresenterSelector ) instance.


1 Answers

Recently, I have also used MVP in my application. I found same issue and pass data from ItemListActivity to ItemDetailActivity using intent. I passed model class which implements Parcelable interface and found no issue, it works fine.

The reason is : In ItemDetailActivity, we have to fetch data from database or from repository in your case which increases one operation in your application.

In ItemlistActivity you will perform only single operation to get all the data. While, if you pass data from ItemListActivity to ItemDetailActivity than, you just have to get data in ItemDetailActivity without any special operation.

I suggest you to go for Solution 1.

like image 86
Kishan S. Rakholiya Avatar answered Oct 23 '22 05:10

Kishan S. Rakholiya