0

onActivityResult should be delegated to the Presenter. Then the Presenter should decide what to do with the result. The question is how to do it in the clean way?

There is an example app in Google samples: https://github.com/googlesamples/android-architecture/blob/todo-mvp-dagger/todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/tasks/TasksFragment.java#L115-L118

It delegates the result to the Presenter:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    mPresenter.result(requestCode, resultCode);
}

But there is a bug in this solution. The Presenter drops the View (in this case the Fragment) in onDestroy() method:

@Override
public void onDestroy() {
    super.onDestroy();
    mPresenter.dropView();  
}

And takes the View in onResume() method:

@Override
public void onResume() {
    super.onResume();
    mPresenter.takeView(this);
}

When we open new activity for the result. Then rotate the phone, then finish current activity and come back to the first one, the Presenter won't have the View because onActivityResult will be called before onResume.

D/TasksFragment: onCreate
D/TasksFragment: onCreateView
D/TasksFragment: onStart
D/TasksFragment: onResume
--- open new activity for the result ---
D/TasksFragment: onPause
D/TasksFragment: onStop
--- rotate the phone ---
--- finish current activity ---
D/TasksFragment: onDestroy
D/TasksFragment: onCreate
D/TasksFragment: onCreateView
D/TasksFragment: onStart
D/TasksFragment: onActivityResult (called before onResume)
D/TasksFragment: onResume
AppiDevo
  • 3,195
  • 3
  • 33
  • 51
  • add mPresenter.takeView(this); instead to oncreate method – has19 Jan 07 '19 at 11:34
  • a better approach is to use architecture components based on mvvm design pattern .in this approach you don't need to handle anything related to android lifecycle – has19 Jan 07 '19 at 11:39

0 Answers0