Eventbus: Allow registration of classes without any subscriptions

Created on 27 Mar 2016  路  3Comments  路  Source: greenrobot/EventBus

For automatic registration in base classes where some defined children might not have any subscriptions. This could be a configuration value on the bus or a registerOptional method.

Most helpful comment

Because I'm honestly just not interested in keeping track of which child class is registering on the bus and which is not. I want to add a subscription to any activity/fragment at any time and have it _just work_. Whatever tiny performance overhead this adds is worth the developer comfort.

If anyone else is looking for this, my current workaround is to add a subscription on a dummy class inside the base class.

All 3 comments

:+1:

Why not move the register call to an overridable method that you overwrite empty in the child classes that should not register to EventBus? Would make it much more clear which classes register and which not and void the performance overhead of searching for subscriber methods.

Something like:

public abstract class BaseClass {
    protected void registerEventBus() {
        EventBus.getDefault().register(this);
    }
}
public class ChildClass extends BaseClass {
    @Override
    protected void registerEventBus() {
        // do not register to EventBus
    };
}

-ut

Because I'm honestly just not interested in keeping track of which child class is registering on the bus and which is not. I want to add a subscription to any activity/fragment at any time and have it _just work_. Whatever tiny performance overhead this adds is worth the developer comfort.

If anyone else is looking for this, my current workaround is to add a subscription on a dummy class inside the base class.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lalit3686 picture lalit3686  路  9Comments

lordcodes picture lordcodes  路  3Comments

cckroets picture cckroets  路  16Comments

ANewAnonymous picture ANewAnonymous  路  4Comments

william-ferguson-au picture william-ferguson-au  路  6Comments