简体   繁体   中英

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?

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. Could this lead to issues? Does this not follow 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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