Karpachev  Igor Karpachev Igor - 5 months ago 12
Android Question

How to prevent calling all 'onEvent' methods in class hierarchy

I am struggling with preventing call all methods in class hierarchy chain.
Lets say i have a base class:

class BaseModel

{ /* Some basic fields goes here */ }

class ModelCompany extends BaseModel

{ /* Fields goes here */ }


Then I want to post two different events:

BaseModel oneEvent = new BaseModel();

ModelCompany otherEvent = new ModelCompany();

EventBus.getDefault().post(oneEvent);

EventBus.getDefault().post(otherEvent);





Somewhere in the activity:

onEvent(BaseModel ev1){}

onEvent(ModelCompany ev2){}





The thing is that both onEvent method will be executed in this case. How to prevent it and post message to exact method?

Answer

From EventBus Source, it is recursively looking for super class:

while (clazz != null) {
    .....
    ..... 
    clazz = clazz.getSuperclass();
}

I don't think there is a setting to disable this, as of now.

Comments