Search code examples
androidmvp

Android Passing data between Interactors MVP


Good day developers, I'm having trouble figuring out how and what should I do correctly so that I won't violate the MVP principle so I have the following situation.

A view that shows List of object, when a particular object is clicked my ViewHolder tells the Presenter about the click event.

What I want is ,when the object is clicked I want to start a new Activity that will display that particular object that was clicked.

Option 1.

Pass the id of the clicked object to Interactor(How should I do it I have no idea) of newly started activity, where a new api call will be called and will return a result based on given id.

Option 2.

On the other hand if I don't want to make a new api call and would like to simply pass the Object to the Iterator of new activity.

(What is better for performance to make a new api call or pass the object around?)

If any of my options seems somehow okay, then how should I implement them correctly?

If they are not then what is a alternative way of doing it?


Solution

  • Your best option is to use the new Blueprints which include Viewmodels in the form of LiveData, and there are specific mechanisms in the viewmodel for sharing the model between views. Basically the model is held with the key being a view id, but if you have a view and then a number of fragments, they can all access it.

    Technically you have control over the key:

    final UserModel viewModel = ViewModelProviders.of(this).get(UserModel.class);

    What I like about this is it solves the scoping problem that has haunted MVC: either use Singletons or pass data around all over the place.