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 fieldprivate 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