public final class EventBus
extends java.lang.Object
post(Object)
to the
bus, which delivers it to subscribers that have matching handler methods for the event type. To receive events,
subscribers must register themselves to the bus using the register(Object)
method. Once registered,
subscribers receive events until the call of unregister(Object)
. By default, subscribers will handle events
in methods named "onEvent".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 void |
clearCaches()
For unit test primarily.
|
static void |
clearSkipMethodNameVerifications()
For unit test primarily.
|
void |
configureLogSubscriberExceptions(boolean logSubscriberExceptions)
Before registering any subscribers, use this method to configure if EventBus should log exceptions thrown by
subscribers (default: true).
|
static EventBus |
getDefault()
Convenience singleton for apps using a process-wide EventBus instance.
|
java.lang.Object |
getStickyEvent(java.lang.Class<?> eventType)
Gets the most recent sticky event for the given type.
|
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 |
register(java.lang.Object subscriber,
java.lang.Class<?> eventType,
java.lang.Class<?>... moreEventTypes)
Like
register(Object) , but only registers the subscriber for the given event types. |
void |
register(java.lang.Object subscriber,
java.lang.String methodName)
Like
register(Object) , but allows to define a custom method name for event handler methods. |
void |
register(java.lang.Object subscriber,
java.lang.String methodName,
java.lang.Class<?> eventType,
java.lang.Class<?>... moreEventTypes)
Like
register(Object, String) , but only registers the subscriber for the given event types. |
void |
registerSticky(java.lang.Object subscriber)
Like
register(Object) , but also triggers delivery of the most recent sticky event (posted with
postSticky(Object) ) to the given subscriber. |
void |
registerSticky(java.lang.Object subscriber,
java.lang.Class<?> eventType,
java.lang.Class<?>... moreEventTypes)
Like
registerSticky(Object) , but only registers the subscriber for the given event types. |
void |
registerSticky(java.lang.Object subscriber,
java.lang.String methodName)
Like
registerSticky(Object) , but allows to define a custom method name for event handler methods. |
void |
registerSticky(java.lang.Object subscriber,
java.lang.String methodName,
java.lang.Class<?> eventType,
java.lang.Class<?>... moreEventTypes)
Like
registerSticky(Object, String) , but only registers the subscriber for the given event types. |
java.lang.Object |
removeStickyEvent(java.lang.Class<?> eventType)
Remove and gets the recent sticky event for the given type.
|
boolean |
removeStickyEvent(java.lang.Object event)
Removes the sticky event if it equals to the given event.
|
static void |
skipMethodNameVerificationFor(java.lang.Class<?> clazz)
Method name verification is done for methods starting with onEvent to avoid typos; using this method you can
exclude subscriber classes from this check.
|
void |
unregister(java.lang.Object subscriber)
Unregisters the given subscriber from all event classes.
|
void |
unregister(java.lang.Object subscriber,
java.lang.Class<?>... eventTypes)
Unregisters the given subscriber for the given event classes.
|
public EventBus()
getDefault()
.public static EventBus getDefault()
public static void clearCaches()
public static void skipMethodNameVerificationFor(java.lang.Class<?> clazz)
public static void clearSkipMethodNameVerifications()
public void configureLogSubscriberExceptions(boolean logSubscriberExceptions)
public void register(java.lang.Object subscriber)
unregister(Object)
once they are
no longer interested in receiving events.
Subscribers have event handling methods that are identified by their name, typically called "onEvent". Event
handling methods must have exactly one parameter, the event. If the event handling method is to be called in a
specific thread, a modifier is appended to the method name. Valid modifiers match one of the ThreadMode
enums. For example, if a method is to be called in the UI/main thread by EventBus, it would be called
"onEventMainThread".public void register(java.lang.Object subscriber, java.lang.String methodName)
register(Object)
, but allows to define a custom method name for event handler methods.public void registerSticky(java.lang.Object subscriber)
register(Object)
, but also triggers delivery of the most recent sticky event (posted with
postSticky(Object)
) to the given subscriber.public void registerSticky(java.lang.Object subscriber, java.lang.String methodName)
registerSticky(Object)
, but allows to define a custom method name for event handler methods.public void register(java.lang.Object subscriber, java.lang.Class<?> eventType, java.lang.Class<?>... moreEventTypes)
register(Object)
, but only registers the subscriber for the given event types.public void register(java.lang.Object subscriber, java.lang.String methodName, java.lang.Class<?> eventType, java.lang.Class<?>... moreEventTypes)
register(Object, String)
, but only registers the subscriber for the given event types.public void registerSticky(java.lang.Object subscriber, java.lang.Class<?> eventType, java.lang.Class<?>... moreEventTypes)
registerSticky(Object)
, but only registers the subscriber for the given event types.public void registerSticky(java.lang.Object subscriber, java.lang.String methodName, java.lang.Class<?> eventType, java.lang.Class<?>... moreEventTypes)
registerSticky(Object, String)
, but only registers the subscriber for the given event types.public void unregister(java.lang.Object subscriber, java.lang.Class<?>... eventTypes)
public void unregister(java.lang.Object subscriber)
public void post(java.lang.Object event)
public void postSticky(java.lang.Object event)
registerSticky(Object)
or
getStickyEvent(Class)
.public java.lang.Object getStickyEvent(java.lang.Class<?> eventType)
postSticky(Object)
public java.lang.Object removeStickyEvent(java.lang.Class<?> eventType)
postSticky(Object)
public boolean removeStickyEvent(java.lang.Object event)
Available under the Apache License, Version 2.0 - Copyright © 2012-2013 greenrobot.de. All Rights Reserved.