public class EventBus
extends java.lang.Object
post(Object)
) to the
bus, which delivers it to subscribers that have a matching handler method for the event type. To receive events,
subscribers must register themselves to the bus using register(Object)
. Once registered, subscribers
receive events until unregister(Object)
is called. Event handling methods must be annotated by
Subscribe
, must be public, return nothing (void), and have exactly one parameter
(the event).Modifier and Type | Field and Description |
---|---|
static java.lang.String |
TAG
Log tag, apps may override it.
|
Constructor and Description |
---|
EventBus()
Creates a new EventBus instance; each instance is a separate scope in which events are delivered.
|
Modifier and Type | Method and Description |
---|---|
static EventBusBuilder |
builder() |
void |
cancelEventDelivery(java.lang.Object event)
Called from a subscriber's event handling method, further event delivery will be canceled.
|
static void |
clearCaches()
For unit test primarily.
|
static EventBus |
getDefault()
Convenience singleton for apps using a process-wide EventBus instance.
|
<T> T |
getStickyEvent(java.lang.Class<T> eventType)
Gets the most recent sticky event for the given type.
|
boolean |
hasSubscriberForEvent(java.lang.Class<?> eventClass) |
boolean |
isRegistered(java.lang.Object subscriber) |
void |
post(java.lang.Object event)
Posts the given event to the event bus.
|
void |
postSticky(java.lang.Object event)
Posts the given event to the event bus and holds on to the event (because it is sticky).
|
void |
register(java.lang.Object subscriber)
Registers the given subscriber to receive events.
|
void |
removeAllStickyEvents()
Removes all sticky events.
|
<T> T |
removeStickyEvent(java.lang.Class<T> eventType)
Remove and gets the recent sticky event for the given event type.
|
boolean |
removeStickyEvent(java.lang.Object event)
Removes the sticky event if it equals to the given event.
|
java.lang.String |
toString() |
void |
unregister(java.lang.Object subscriber)
Unregisters the given subscriber from all event classes.
|
public EventBus()
getDefault()
.public static EventBus getDefault()
public static EventBusBuilder builder()
public static void clearCaches()
public void register(java.lang.Object subscriber)
unregister(Object)
once they
are no longer interested in receiving events.
Subscribers have event handling methods that must be annotated by Subscribe
.
The Subscribe
annotation also allows configuration like ThreadMode
and priority.public boolean isRegistered(java.lang.Object subscriber)
public void unregister(java.lang.Object subscriber)
public void post(java.lang.Object event)
public void cancelEventDelivery(java.lang.Object event)
Subscribe.priority()
). Canceling is restricted to event handling methods running in posting thread
ThreadMode.POSTING
.public void postSticky(java.lang.Object event)
Subscribe.sticky()
.public <T> T getStickyEvent(java.lang.Class<T> eventType)
postSticky(Object)
public <T> T removeStickyEvent(java.lang.Class<T> eventType)
postSticky(Object)
public boolean removeStickyEvent(java.lang.Object event)
public void removeAllStickyEvents()
public boolean hasSubscriberForEvent(java.lang.Class<?> eventClass)
public java.lang.String toString()
toString
in class java.lang.Object
Available under the Apache License, Version 2.0 - Copyright © 2012-2016 greenrobot.org. All Rights Reserved.