3

I have a BaseFragment class which registers/unregisters itself to EventBus in onStart()/onStop(), and several child classes that inherit from it (FragmentA, FragmentB...). The base class doesn't have any methods annoted with @Subscribe, and FragmentA neither, but I want it to register anyway in case I add some in the future.

If I register an object that doesn't have any @Subscribe annotated method, I except nothing to happen, like when using SquareUp's Otto, but EventBus throws an exception :

org.greenrobot.eventbus.EventBusException: Subscriber class com.company.app.FragmentA and its super classes have no public methods with the @Subscribe annotation

Why throw an exception ? I mean, it doesn't prevent the app to run.

Is the only solution to wrap the register()/unregister() calls in BaseFragment into an ugly try ... catch or is there a cleaner workaround?

Mickäel A.
  • 9,012
  • 5
  • 54
  • 71

2 Answers2

2

If you want your child fragments to be able to have no @Subscribe method without any exception to be thrown, just put an empty subscriber in the base class :

@Subscribe
public void dummyEvent(SomeClassThatWillNeverBePosted event) {}
Mickäel A.
  • 9,012
  • 5
  • 54
  • 71
Atiq
  • 14,435
  • 6
  • 54
  • 69
0

I think you should register/unregister seperately for each fragment. Because if you do this in base fragment your child fragments register same events but each fragment has different events.

faranjit
  • 1,567
  • 1
  • 15
  • 22