androidviewmvpandroid-contextpresenter

Android - context with MVP


I'm about creating a simple app with MVP implementation, and trying to make a permission request in a presenter. To make a permission request, I need to pass Context like this.

        // Location permission has not been granted yet, request it.
        ActivityCompat.requestPermissions(fragmentActivity, new String[]{permission}, requestId);

I've read several articles and they mention that using Context in a presenter is not a good exercise. So, I'm just wondering how people are handling a permission request with MVP. And I don't really know why using Context in a presenter is not good practice. Please, help me to understand how I should handle a permission request and why using context is not good practice.

Thanks


Solution

  • You must never send any object related to Android to the presenter layer, and they must be fully decoupled.

    to do these things I always remember a good sentence and that goes Do not inject objects, inject operations and behavior.

    it so simple dont inject your context into your presenter which is a wrong practice. instead in your view contract (view interface) add a function called getPermission() then in your view implement that method along with other methods of your contract, then call that method whenever you need the permission.

    thats the best way. trust me ;)