Search code examples
androidandroid-fragmentsgreenrobot-eventbus

OnEvent cast incorrect event Object using EventBus


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


Solution

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