简体   繁体   English

Android - 与MVP的上下文

[英]Android - context with MVP

I'm about creating a simple app with MVP implementation, and trying to make a permission request in a presenter. 我是关于使用MVP实现创建一个简单的应用程序,并尝试在演示者中发出权限请求。 To make a permission request, I need to pass Context like this. 要发出权限请求,我需要像这样传递Context。

        // 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. 我已经阅读了几篇文章,他们提到在演示者中使用Context并不是一个好的练习。 So, I'm just wondering how people are handling a permission request with MVP. 所以,我只是想知道人们如何使用MVP处理权限请求。 And I don't really know why using Context in a presenter is not good practice. 我真的不知道为什么在演示者中使用Context并不是一个好习惯。 Please, help me to understand how I should handle a permission request and why using context is not good practice. 请帮助我理解我应该如何处理权限请求以及为什么使用上下文不是好习惯。

Thanks 谢谢

You must never send any object related to Android to the presenter layer, and they must be fully decoupled. 您绝不能将与Android相关的任何对象发送到演示者层,并且它们必须完全解耦。

to do these things I always remember a good sentence and that goes Do not inject objects, inject operations and behavior . 做这些事情我总是记得一个好的句子,那就是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. 而在您的视图合约(视图界面)中添加一个名为getPermission()的函数,然后在您的视图中实现该方法以及合同的其他方法,然后在需要权限时调用该方法。

thats the best way. 这是最好的方式。 trust me ;) 相信我 ;)

There are multiple flavors of MVP out there in practice. 实践中有多种MVP。 I am not here to start a debate which one is right and which one is wrong. 我不是来这里开始辩论哪一个是正确的,哪一个是错的。 As long as a particular flavor works for your scenario, it shall be used. 只要特定风味适用于您的场景,就应该使用它。

Instead, I will try to explain why the context in Presenter should be avoided and one of the ways I have avoided in my code. 相反,我将尝试解释为什么应该避免Presenter中的上下文以及我在代码中避免使用的方法之一。

One of the major reason you should not have a context in the presenter, there might be references to presenter which could leak the activity. 您不应该在演示者中有上下文的主要原因之一是,可能会引用可能泄漏活动的演示者。 In places where I had to deal with a context within the activity, I have accessed through Views. 在我必须处理活动中的上下文的地方,我通过视图访问。

interface View {
   Context getContext();
}

interface Presenter {
   void setView(View view);
}

So PresenterImpl implements a view, onCreate of the activity and resets it onDestroy of the activity. 因此,PresenterImpl实现了一个视图,onCreate活动并将其重置为活动的Destroy。 So the presenter never directly holds the context. 因此,演示者永远不会直接持有上下文。 But it holds the view, which has knowledge about the View. 但它持有的观点,其中有关于视图的知识。

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

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