how to identify caller for eventbus on activity class

150 views Asked by At

i am using EventBus on activity and override one event ABC. now i am calling that event from multiple classes (EventBus.getDefault().post(new ABC()) etc.) and i am getting callback on my activity class. so my question is : is there any way to identify caller who called that event on my activity class.

2

There are 2 answers

0
CommonsWare On

Put something in ABC that tells you where the event originated from, such as via a constructor parameter.

0
Konstantin Kiriushyn On

I'd suggest adding a constructor which receives a tag

public ABC(int tag) {}

And then in your Activity you can proceed like this:

@Subscribe()
public void onEvent(ABC event) {
    switch(event.getTag()) {
        case MyService.TAG: 
            //process data from your service
            break;
        case MyFragment.TAG: 
            //process data from your fragment
            break;
    }
}