Package org.greenrobot.eventbus
Class EventBus
- java.lang.Object
-
- org.greenrobot.eventbus.EventBus
-
public class EventBus extends java.lang.Object
EventBus is a central publish/subscribe event system for Java and Android. Events are posted (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 usingregister(Object)
. Once registered, subscribers receive events untilunregister(Object)
is called. Event handling methods must be annotated bySubscribe
, must be public, return nothing (void), and have exactly one parameter (the event).
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
TAG
Log tag, apps may override it.
-
Constructor Summary
Constructors Constructor Description EventBus()
Creates a new EventBus instance; each instance is a separate scope in which events are delivered.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method 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.Logger
getLogger()
For internal use only.<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.
-
-
-
Constructor Detail
-
EventBus
public EventBus()
Creates a new EventBus instance; each instance is a separate scope in which events are delivered. To use a central bus, considergetDefault()
.
-
-
Method Detail
-
getDefault
public static EventBus getDefault()
Convenience singleton for apps using a process-wide EventBus instance.
-
builder
public static EventBusBuilder builder()
-
clearCaches
public static void clearCaches()
For unit test primarily.
-
register
public void register(java.lang.Object subscriber)
Registers the given subscriber to receive events. Subscribers must callunregister(Object)
once they are no longer interested in receiving events. Subscribers have event handling methods that must be annotated bySubscribe
. TheSubscribe
annotation also allows configuration likeThreadMode
and priority.
-
isRegistered
public boolean isRegistered(java.lang.Object subscriber)
-
unregister
public void unregister(java.lang.Object subscriber)
Unregisters the given subscriber from all event classes.
-
post
public void post(java.lang.Object event)
Posts the given event to the event bus.
-
cancelEventDelivery
public void cancelEventDelivery(java.lang.Object event)
Called from a subscriber's event handling method, further event delivery will be canceled. Subsequent subscribers won't receive the event. Events are usually canceled by higher priority subscribers (seeSubscribe.priority()
). Canceling is restricted to event handling methods running in posting threadThreadMode.POSTING
.
-
postSticky
public void postSticky(java.lang.Object event)
Posts the given event to the event bus and holds on to the event (because it is sticky). The most recent sticky event of an event's type is kept in memory for future access by subscribers usingSubscribe.sticky()
.
-
getStickyEvent
public <T> T getStickyEvent(java.lang.Class<T> eventType)
Gets the most recent sticky event for the given type.- See Also:
postSticky(Object)
-
removeStickyEvent
public <T> T removeStickyEvent(java.lang.Class<T> eventType)
Remove and gets the recent sticky event for the given event type.- See Also:
postSticky(Object)
-
removeStickyEvent
public boolean removeStickyEvent(java.lang.Object event)
Removes the sticky event if it equals to the given event.- Returns:
- true if the events matched and the sticky event was removed.
-
removeAllStickyEvents
public void removeAllStickyEvents()
Removes all sticky events.
-
hasSubscriberForEvent
public boolean hasSubscriberForEvent(java.lang.Class<?> eventClass)
-
getLogger
public Logger getLogger()
For internal use only.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-