I try use MVP in my project and I get some question.
For example I have
MyPresenter presenter;
MyFragment view;
In view I have button
with name "open barcode scan" witch is open barcode scan activity like this:
IntentIntegrator.forSupportFragment(this).initiateScan()
Hwo must cal this code? view
or presenter
?
1 View realization:
scanButton.setOnClickListener(view -> IntentIntegrator.forSupportFragment(this).initiateScan());
2 presenter realization:
scanButton.setOnClickListener(view -> presenter.openScan());
And next question - How must process onActivityResult
? and How?
for example
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode){
case
...
case
...
case
//it is logic
}
EDIT
1 way
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode){
case
presenter.callMethod1()
case
presenter.callMethod2()
case
//it is logic
}
2 way
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
presenter.onActivityResult(int requestCode, int resultCode, Intent data);
}
The Presenter
acts as the bridge or the middle man between View
and Model
, so it must be called on the View.
onActivityResult
will be as is on your fragment
. and then, if you need to save the result from the scan, that's the time you need to call the presenter
.
A presenter typically hosts business logic associated with a particular feature, and the corresponding view handles the Android UI work. The view contains almost no logic; it converts the presenter's commands to UI actions, and listens for user actions, which are then passed to the presenter.
Reference: https://github.com/googlesamples/android-architecture/tree/todo-mvp/
You might be mistaken the onActivityResult
is where the business logic is, it is not. That's why you need to pass it to the presenter
when you get the result so the presenter will process (business logic) it and then give it to your repository that will save it.