Reputation: 3063
As I understand, the method hasSubscriberForEvent(Class<?> eventClass)
from EventBus should return true if I'm listening for any Event on this eventClass
. But it always returns false, I'm not sure why.
In my fragment I register and unregister the EventBus, and declare the method to listen the Post:
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
EventBus.getDefault().register(this);
super.onCreate(savedInstanceState);
}
@Override
public void onDestroy() {
EventBus.getDefault().unregister(this);
super.onDestroy();
}
public void onEvent(MyDummyObject event) {
Log.i(TAG,"event received!");
}
In my service I need to check if this Fragment is visible or not, one way to do it I assumed would be using EventBus.getDefault().hasSubscriberForEvent(MyFragment.class)
.
I expected that if EventBus would see that MyFragment
has anything registered would return true, otherwise would return false.
Am I missing something, or hasSubscriberForEvent()
does not behave this way?
Note: If I do EventBus.getDefault().post(new MyDummyObject());
the event fires, so I assume the EventBus is being registered and unregistered successfully.
Upvotes: 0
Views: 838
Reputation: 3063
It seems that I misunderstood the method hasSubscriberForEvent
!
I had to change:
EventBus.getDefault().hasSubscriberForEvent(MyFragment.class);
to:
EventBus.getDefault().hasSubscriberForEvent(MyDummyObject.class)
The parameter is the object type we are listening and not the class that is registering and unregistering the EventBus.
For more info check the request for this feature in this link.
Upvotes: 3