Search code examples
androideventsevent-busotto

Otto Event Bus: different Event Classes


How does Otto handle different event classes? Is it possible to have different event classes?

Would only the listeners that listen to the specific event class get notified? E.g. would the sample below work, with only the listener in class A being notified? Assume that EventClassA and EventClassB does not extend the same superclass.

class A {
    @Subscribe
    public void handleEvent(EventClassA event)
    {
            //
    }
}

class B {
    @Subscribe
    public void handleEvent(EventClassB event)
    {
            //
    }
}

class C {
    public void postEvent() {
        bus.post(new EventClassA());
    }
}

Solution

  • Is it possible to have different event classes?

    Yes.

    Would only the listeners that listen to the specific event class get notified?

    Yes. In your sample, an instance of A that is subscribed to the bus will be called with handleEvent(), while an instance of B that is subscribed to the bus will not be called.