简体   繁体   English

主持人是否应该了解Android MVP架构中的“活动”?

[英]Should the Presenter know about the Activity in Android MVP architecture?

I am wondering if the Activity should be referenced or not within Presenter code when using the Android MVP architecture? 我想知道使用Android MVP架构时是否应该在Presenter代码中引用Activity

The example MVP architecture that I have found so far doesn't reference it, but in my code it's not a property on the Presenter, but an argument in some methods. 到目前为止,我发现的示例MVP架构并未引用它,但是在我的代码中,它不是Presenter的属性,而是某些方法中的参数。 Could this lead to issues? 这会导致问题吗? Does this not follow Android MVP? 这不遵循Android MVP吗?

Here is a code example from one Presenter: 这是来自一个演示者的代码示例:

package com.example.example;

import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.FileProvider;

import com.example.example.util.Constants;
import com.example.example.util.ImageFile;

import java.io.IOException;

/**
 * Presenter from home screen, (Main), of the app
 */
public class MainPresenter implements MainContract.Presenter {

    private final MainContract.View mView;
    private final ImageFile mImageFile;

    public MainPresenter(MainContract.View mainView, ImageFile imageFile) {
        mView = mainView;
        mImageFile = imageFile;
    }

    @Override
    public void takePicture(FragmentActivity activity) throws IOException {
        mImageFile.create(activity);

        Uri photoUri = FileProvider.getUriForFile(
                activity,
                Constants.FILE_PROVIDER_PATH,
                mImageFile.getFile());

        mView.openCamera(photoUri);
    }

    Uri getImageFileUri() {
        return  mImageFile.getUri();
    }
}

In proper MVP implementation, Presenter should not know about the activity. 在正确的MVP实施中,Presenter不应了解该活动。 If we'll use activity then we'll have to mock the activity during testing, that'll make the testing difficult. 如果要使用活动,则必须在测试过程中模拟活动,这将使测试变得困难。 So, in your case, you should pass your mImageFile to activity through the view reference and create the URI inside activity class. 因此,在您的情况下,您应该通过视图引用将mImageFile传递给活动,并在活动类内部创建URI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM