Events being received multiple times - Greenrobot eventbus
Are your register/unregister calls paired correctly? E.g. if you register()
in Activity.onResume()
, are you calling unregister()
in Activity.onPause()
.
Closing all activities does not kill your process. I.e. all registered classes are still there, you have to explicitly clean up and unregister from the event bus, or reuse them when the Activity comes back.
This is old, but just in case anyone has this problem also: Tread lightly when using EventBus inside dynamically generated things like Fragments or other classes; I didn't really understand why they were posting to the EventBus more than once, but I think it had to do with this (I had more than one dynamically generated Fragment). It worked normally once I put the register()
, unregister()
, onEvent()
into the parent Activity code (which conveniently also uses onPause()
and onResume()
).
Same thing happening in my case when I am using the
EventBus.getDefault().postSticky(new Event("Hii !"));
for sending the event.
The event is received multiple times when I come to that activity.
So I fixed this by removing the event after receiving in onEvent method. This solved my problem.
Used: removeStickyEvent(object)
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEvent(Event event) {
/* Do something */
EventBus.getDefault().removeStickyEvent(event);
}