3

I am using Otto and Dagger. Some of my events are being received multiple times on only one post.

In my view that posts the event:

@Inject Bus mBus;

In the constructor:

((MyApplication) mContext.getApplicationContext()).inject(this);  

view.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(final View view) {
    Log.d(TAG, "Sending SearchResultClickedEvent.");
    mBus.post(new SearchResultClickedEvent(mModel.getPlaceId()));
  }
});

In the super class for my subscribing activity:

@Inject protected Bus mBus;

In the onCreate():

((HarryApplication) getApplicationContext()).inject(this);

In the subscribing activity:

@Subscribe
public void on(SearchResultsRecyclerViewHolder.SearchResultClickedEvent event) {
  Log.d(TAG, "SearchResultClickedEvent received.");
}

The logs after a single click:

03-26 12:59:51.496  24613-24613/D/SearchResultView﹕ Sending SearchResultClickedEvent.
03-26 12:59:51.496  24613-24613/D/Subscriber﹕SearchResultClickedEvent received.
03-26 12:59:51.497  24613-24613/D/Subscriber﹕SearchResultClickedEvent received.
03-26 12:59:51.499  24613-24613/D/Subscriber﹕SearchResultClickedEvent received.
clocksmith
  • 6,226
  • 3
  • 32
  • 45
  • 2
    is there any chance you have your activity leaked and you have multiple receivers registered. Please show us how you subscribe & unsubscribe from the bus? – Pavel Dudka Mar 26 '15 at 18:26
  • Ah thank you! I didn't know we had to unregister. I updated my answer. – clocksmith Mar 26 '15 at 18:36

1 Answers1

7

I was not unregistering my event bus object.

Added this and it works as expected:

  @Override protected void onPause() {
    super.onPause();
    mBus.unregister(this);
  }
clocksmith
  • 6,226
  • 3
  • 32
  • 45
  • THANK YOU. I meet the same problem, I thought it was memory leak until I read your answer. – Shaw Nov 06 '15 at 08:56