9

I am using Greenrobot's EventBus in my app, and it works fine.

However, if I press the back button to close the app, then restart the app instantly I seem to receive the event twice. If I then do so again, I will receive it three times and so on.

I am checking with logs and debugging to see if I have multiple instances of any classes, or if I am registering multiple times, but I can't see any extra classes and using isRegistered returns false.

Any ideas?

Thanks

Russ Wheeler
  • 2,590
  • 5
  • 30
  • 57
  • can you post your code? – Orhan Obut Jan 23 '15 at 23:36
  • 1
    I agree with crazymaik -- it feels like you are missing an `unregister()` call somewhere. Bear in mind that the BACK button does not "close the app", insofar as your process is still around, at least for a while. BACK just destroys the foreground activity. – CommonsWare Jan 24 '15 at 00:47

5 Answers5

16

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.

Maik
  • 3,419
  • 1
  • 23
  • 34
  • I think you're right, I've done a grep and my registers outnumber my unregisters. However, I am not sure if I am using this correctly. I see now I should definitely have added more info to the question originally. I am trying to write a game using LibGDX, which is written pretty much in pure Java. In this repsect I am not using Activities/Fragments as such, and maybe I am misusing eventbus. I am using it in normal Java classes. I am registering in the constructor, but I don't know where to put the unregister. I am unaware of a method that get's called on GC of a class? – Russ Wheeler Jan 24 '15 at 18:42
  • I have just added a call to isRegistered in the class that I am not unregistering, and I am receiving false, which would go to say that it is not still registered? Or is this false, and there are now two classes on the heap, the old one, still registered, and this new one? – Russ Wheeler Jan 24 '15 at 18:45
  • I think I've got it working. I added a method called unRegister() in all my classes that were using EventBus and that weren't activities or that didn't have the LibGDX dispose() method in them. Then I had to meticulously make sure that everything was unregistered when the game was closed. It appears to be working, but I'll come back to you if it isn't. Thanks all – Russ Wheeler Jan 24 '15 at 19:11
4

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()).

mz496
  • 790
  • 1
  • 7
  • 13
2

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);
}
tejraj
  • 300
  • 2
  • 4
  • 16
1

The problem was not that the event was actually fired multiple times, but that the handler was invoked multiple times. As seen in the code above, the bus.register method is called everytime I create the object; because of the activities lifecycle, this happened multiple times, causing the handler to be invoked multiple times.

madhu527
  • 4,644
  • 1
  • 28
  • 29
0

I had a specific case that I want to share. Maybe it helps someone else. While we are using a parent activity for all of our Activities in our project, we register and unregister EvenBus for each activity inside the parent class. In one of our activities, we were calling EventBas before invoking the previous activity's EventBus. Then we had twice trigger

iman kazemayni
  • 1,255
  • 1
  • 19
  • 20