0

I have register EventBus on both Activity (BaseActivity) and Fragment (BaseFragment). So, I catch event with: In BaseActivity, and BaseFragment I have same code:

public void onEvent(Object object) {
    // do nothing
}

In my child fragment (A) extended from BaseFragment, I have a event for it XYZEvent, and I expected that this method is called:

public void onEvent(XYZEvent event) {
    // my logic, not called :(
}

But it did not. I have debugged, both onEvent in BaseActivity and BaseFragment are called. So, I changed my fragment (A) like below:

public void onEvent(Object event) {
   if(event instanceof XYZEvent) {
       // my logic
       // after changed this block code is called (~_~), why???
   }
}

It's called. So, I want to know what am I missed when onEvent(XYZEvent event) not fired? but onEvent(Object event) did. Seem returned event has wrong casting, my XYZEvent was casted to Object.

P/S: I used this library compile 'de.greenrobot:eventbus:2.4.0' and XYZEvent contains an Serializable object

NamNH
  • 1,752
  • 1
  • 15
  • 37
  • What I would guess is the event is pass with a casting to force the parameter to be an Object (strange but should explains this). So on call, `onEvent((Object)event)` would take the closest definition to `onEvent(Object)`, the one from BaseFragment. This need to be checked – AxelH Nov 29 '16 at 06:39
  • Thanks for your response, but the event is `XYZEvent` why closest to `Object` here? Also, if I test by commented out `onEvent(Object event)` in `BaseActivity` and `BaseFragment`, my `onEvent(XYZEvent event)` still not fired. The only way to make it called is `onEvent(Object event)`. – NamNH Nov 29 '16 at 07:05
  • I don't have an Environnement to search but using debugger to find the source of the call, and check the source code, you should find it. [Read this](http://stackoverflow.com/a/26844295/4391450), this might help you ;) – AxelH Nov 29 '16 at 07:21
  • @AxelH After try debug again, I found that `onEvent(XYZEvent event` not in `subscriptions` list. I don't know why? If I remove all `onEvent(object)` my `XYZEvent` still not in `subscriptions` list. – NamNH Nov 29 '16 at 07:56

1 Answers1

0

Ok, this is my mistake. I have a onEvent method in service, it already steal my XYZEvent.

NamNH
  • 1,752
  • 1
  • 15
  • 37