2

I've got 2 running classes registered for EventBus events - instance A and instance B. Both of them have implemented onEvent(SampleEvent event) method for receiving EventBus.post(new SampleEvent(int foo, String bar)); from a place C. So far so good.

But there is a time when I need to post SampleEvent from B to A. Is it possible to tell B that it shouldn't process SampleEvent sent by itself, or I need to do workaround like setting a flag in SampleEvent?

Jakub Turcovsky
  • 2,096
  • 4
  • 30
  • 41
  • Why are you not using a separate event when sending an event from B to A? – Peter P Jul 01 '15 at 11:18
  • @PeterP This is also a way. The thing is that it would be totally same event with the same variables, only different name. If my idea isn't possible to acomplish, I would go exactly this way. ;) – Jakub Turcovsky Jul 01 '15 at 11:29

1 Answers1

1

You could try to extend SampleEvent for exmaple SampleEventToA than you check within B by using reflection:

if  (! event instanceof SampleEventA){
    //do things here
}

Found this issue on otto EventBus: Derived events from super event will also call the subscriber of the super event. This would be a possible OO solution!

https://github.com/square/otto/issues/83

Peter P
  • 491
  • 4
  • 14