简体   繁体   English

Android MVP正确发布演示者的方式

[英]Android MVP Proper Way to Release Presenter

I am working with MVP architecture and I'm stuck on how to proper release the presenter. 我正在使用MVP架构,我一直坚持如何正确释放演示者。 First, let me tell you guys what is happening. 首先,让我告诉你们发生了什么。

PROBLEM 问题

1) My presenter make an async server request. 1)我的演示者发出异步服务器请求。

2) When I receive the server response, my view (fragment) was already detached but I still have its instance hold on my presenter (which can causes memory leaks) and I call a method from View to set some data I received from the server. 2)当我收到服务器响应时,我的视图(片段)已经分离,但我仍然在我的演示者上保持其实例(这可能导致内存泄漏),我从View调用一个方法来设置从服务器收到的一些数据。

3) Inside my view I use the context from getActivity() method, which in this stage will return null . 3)在我的视图中,我使用getActivity()方法的上下文,在此阶段将返回null

HOW I TRIED TO FIX THIS PROBLEM 我如何试图解决这个问题

1) When I detach the fragment I call a release() method on my presenter. 1)当我分离片段时,我在演示者上调用了release()方法。 Inside this method I thought about setting the instance of my view to null. 在这个方法中,我考虑将视图的实例设置为null。 This would work but then I would need to add null checks literally everywhere in my presenter checking whether my view was already set to null or not. 这可行,但后来我需要在演示者的每个地方添加空检查,检查我的视图是否已设置为null。 Doesn't seems to be the best approach. 似乎不是最好的方法。

2) In my view (fragment), check whether getActivity() is null before using it. 2)在我的视图(片段)中,在使用之前检查getActivity()是否为null。 But it doesn't fix the memory leak problem and I would need to add this check literally everywhere in my Fragment(s); 但它并没有解决内存泄漏问题,我需要在我的片段中随处添加这个检查;


Do you guys have an alternative for that? 你们有替代品吗? Is that a proper way to release my presenter when my fragment is detached in a way that whenever my presenter call a method on my view I will be sure that view is attached to an Activity? 当我的片段被分离时,这是一种释放我的演示者的正确方法,每当我的演示者在我的视图上调用一个方法时,我将确保该视图附加到一个Activity? Is EventBus a good approach for that? EventBus是一个很好的方法吗?

Thanks a lot! 非常感谢!

The one of the main goals of the presenter layer is to be Android Framework independent, that means that you don't have in imports any of the packages from Android Framework, making it pure Java class. presenter层的主要目标之一是独立于Android Framework,这意味着您没有从Android Framework导入任何包,使其成为纯Java类。 You should make your Activity or Fragment implements the ActivityView interface or FragmentView interface, ant let concrete Activity or Fragment implement that interface. 你应该让你的ActivityFragment实现ActivityView接口或FragmentView接口,让具体的ActivityFragment实现那个接口。 Now, in the onCreate() you create an instance of the presenter and pass as an argument the View (Activtity or Fragment), and in Presenter class you will have ActivityView or FragmentView reference, initializing it through the constructor, something like this: 现在,在onCreate()创建一个presenter实例并将View(Activtity或Fragment)作为参数传递,在Presenter类中,您将拥有ActivityViewFragmentView引用,通过构造函数初始化它,如下所示:

Activity.java Activity.java

public class Activity implements ActivityView {
    ...
    private Presenter mPresenter;

    public void onCreate() {
         // some other code

         mPresenter = new Presenter(this);

         // some other code
    }

Presenter.java Presenter.java

    public class Presenter {
         private ActivityView mActivityView;


         public Presenter(ActivityView activityView) {
           this.mActivityView = activityView;
         }
}

Now you can call the methods in the Activity or Fragments , but those methods must be listed in the interface. 现在,您可以调用ActivityFragments方法,但必须在界面中列出这些方法。 In case your are your RxJava2, you can have CompositeDisposable object in the Presenter , adding your network calls to that disposable, and in proper lifecycle methods from Activity or Fragment , your can call dispose() method on compositeDisposable , through Presenter of course. 如果你是你的RxJava2,你可以在Presenter拥有CompositeDisposable对象,将你的网络调用添加到那个一次性,并且在ActivityFragment适当生命周期方法中,你可以通过Presenter调用compositeDisposable上的dispose()方法。 In that way you will clean any ongoing network operations, making it not update the UI if the UI is not present. 这样,您将清除任何正在进行的网络操作,如果UI不存在,则不会更新UI。 Hope this answer helps you :) 希望这个答案可以帮助你:)

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

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