Event Inheritance in EventBus

If you are not using pub/sub framework for your Android client, this post is not for you. But then again, if you are a mobile developer you should ask yourself why your mobile clients applications aren’t using pub/sub framework? This deserve a separate post that will explain the benefits of such architecture. If you are into pub/sub for Android clients you are probably using Otto or EventBus.
EventBusSome of my team were using EventBus and encountered a weird event dispatch situation: some subscribers would get multiple invocations for a single event post.
The code could be simplified to the following sample:

public class MyActivity extends Activity {
   ...
   public class BaseEvent {}
   public class DerivedEvent extends BaseEvent {}
   ...
   @Override
   public void onStart() {
      super.onStart();
      EventBus.getDefault().register(this);
   }

   @Override
   public void onStop() {
      EventBus.getDefault().unregister(this);
      super.onStop();
   }

   public void onEvent(BaseEvent event) {
      // Handle base event
   }

   public void onEvent(DerivedEvent event) {
      // Handle derived event
   }
   ...
}

One could argue the need for inheritance in the events object. If you use EventBus extensively, you will learn that you need a lot of event objects to replace all those listeners that infested your code before you used EventBus. Usually, you will have a lot of events but some of them need to pass the same information to the subscriber (say a String or a List). Writing the boilerplate code for instantiation and getters/setters for each separate event class could be time consuming, and error prone:

public class Evnet1 {
   private final String foo;
   public Event1(String bar) {
      this.foo = bar;
   }
   public String getFoo() {
      return foo;
   }
}

Now imagine repeating this for Event2 to Event100. Not a lovely though, isn’t it?
We would rather have Event2 to Event100 extend Event1 and that’s all. This was the base idea of the code sample presented above for MyActivity.
Now somewhere else in your code there will be a publisher that will do this:

...
EventBus.getDefault().post(new DerivedEvent());
...

And for some reason both onEvent methods in MyActivity will be called! WTF? If you dig into EventBus internals, you will understand why this happens (If you don’t understand this drop me a line). So does it mean we can’t use event inheritance with EventBus? Absolutely not!! The solution is simple: don’t ever subscribe to base event object. This will solve all your problems. i.e.:

...
public abstract class BaseEvent() {}
public final class DerivedEvent1() {}
public final class DerivedEvent2() {}
@Override
public void onEvent(DerivedEvent1 event) {
   // Do something with the event, what you normally intended for the base
   // event in the previous sample
}
@Override
public void onEvent(DerivedEvent2 event) {
   // Do something with the event, as it was the DerivedEvent from the
   // previous sample
}
...

Now if you fire the DerivedEvent2:

EventBus.getDefault().post(new DerivedEvent2());

Only the event handle for this event (line 11) will be called.

To prevent inheriting the derived events, label the classes final, and to prevent instantiating the base event classes label them abstract.