简体   繁体   中英

Otto event bus receiving multiple events on single post

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.

I was not unregistering my event bus object.

Added this and it works as expected:

  @Override protected void onPause() {
    super.onPause();
    mBus.unregister(this);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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