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?
Kamil Seweryn
  • 2,072
  • 2
  • 17
  • 23
  • 1
    the `Application` doesn't really end until Android kills it as per http://stackoverflow.com/a/27193094/2413303 but having a reference that cannot die would mostly be a problem with View hierarchies (Activities/Fragments). – EpicPandaForce May 27 '15 at 15:19
  • EventBus.getDefault() is indeed a static reference, but it holds the reference to presenter which on Activity close is separated from views. Is this considered as memory leak? – Kamil Seweryn May 28 '15 at 08:48
  • also, if you don´t unregister, some UI code you´ve got in one of the bus event handlers may try to call to an already recycled or disposed view – eduyayo Jun 15 '15 at 11:19

1 Answers1

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

Kamil Seweryn
  • 2,072
  • 2
  • 17
  • 23