3

I'm implementing MVP pattern in Android and I'm using EventBus to let know the Presenter from activity "A" that something happen at activity "B" to update views from "A".

I registered the presenter to EventBus inside in constructor but I don't see any place where I could unregister it.

public class PresenterA extends nucleus.presenter.Presenter<ViewA> {

    public PresenterA() {
        EventBus.getDefault().register(this);
    }

    public void onEvent(ChangesEvent e) {
        // change views
    }
}
  1. Is it necessary to unregister at all, when the presenter is suppose to live as long as application (it is not recreated on configuration change)?
  2. When user leaves the application (closes activity "A") will the references be freed or is it a memory leak?
4

1 回答 1

1
  1. Unregister is important and when user leaves application that does not mean the resources are cleaned instantly
  2. Since the EventBus holds static reference to presenter it is not freed until OS kills the process and so, considered as leak.

As for nucleus.presenter.Presenter it will be common to register on onTakeView(ViewType view) and unregister on onDropView() since event handling changes the view

于 2015-06-15T11:16:14.073 回答