While most use cases of EventBus do not need priorities nor event cancellation, they may come in handy in some special cases. For example, an event may trigger some UI logic if the app is in the foreground, but react differently if the app is currently not visible to the user.
Subscriber Priorities
You may change the order of event delivery by providing a priority to the subscriber during registration.
1 2 3 4 | @Subscribe(priority = 1); public void onEvent(MessageEvent event) { ... } |
Within the same delivery thread (ThreadMode), higher priority subscribers will receive events before others with a lower priority. The default priority is 0.
Note: the priority does not affect the order of delivery among subscribers with different ThreadModes!
Cancelling event delivery
You may cancel the event delivery process by calling cancelEventDelivery(Object event) from a subscriber’s event handling method. Any further event delivery will be cancelled, subsequent subscribers won’t receive the event.
1 2 3 4 5 6 7 8 | // Called in the same thread (default) @Subscribe public void onEvent(MessageEvent event){ // Process the event ... // Prevent delivery to other subscribers EventBus.getDefault().cancelEventDelivery(event) ; } |
Events are usually cancelled by higher priority subscribers. Cancelling is restricted to event handling methods running in posting thread ( ThreadMode.PostThread).