在 MVP 项目中正确使用 onActivityResult

correct work with onActivityResult in MVP project

我尝试在我的项目中使用 MVP,但我遇到了一些问题。

例如我有

MyPresenter presenter;
MyFragment view;

在视图中我有 button 名称 "open barcode scan" 是打开的条码扫描 activity 像这样:

IntentIntegrator.forSupportFragment(this).initiateScan()

必须调用此代码吗? viewpresenter?

1 视图实现:

scanButton.setOnClickListener(view -> IntentIntegrator.forSupportFragment(this).initiateScan());

2个presenter实现:

scanButton.setOnClickListener(view -> presenter.openScan());

下一个问题 - 必须如何处理 onActivityResult?以及如何?

例如

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
 switch(requestCode){
  case 
  ...
  case 
  ...
  case 
//it is logic
}

编辑

1 种方式

@Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
     switch(requestCode){
      case 
      presenter.callMethod1()
      case 
      presenter.callMethod2()
      case 
    //it is logic
    }

2路

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

Presenter作为ViewModel之间的桥梁或中间人,所以必须在View上调用它.

onActivityResult 将与您的 fragment 上的一样。然后,如果您需要保存扫描结果,那就是您需要调用 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.

参考:https://github.com/googlesamples/android-architecture/tree/todo-mvp/

您可能会误认为 onActivityResult 是业务逻辑所在的位置,它不是 。这就是为什么您需要在获得结果时将其传递给 presenter,以便演示者将处理(业务逻辑)它,然后将其提供给将保存它的存储库。