public class EventBus extends BroadcastReceiver
Currently, there is a single EventBus that handles EventBus.Event
s for each subscriber
on the main application thread. Publishers can send() events to synchronously call subscribers
of that event, or post() events to be processed in the next run of the Looper
. In
addition, the EventBus supports sending and handling EventBus.InterprocessEvent
s
(within the same package) implemented using standard BroadcastReceiver
mechanism.
Interprocess events must be posted using postInterprocess() to ensure that it is dispatched
correctly across processes.
Subscribers must be registered with a particular EventBus before they will receive events, and handler methods must match a specific signature.
Event method signature:
EventBus.Event
Interprocess-Event method signature:
EventBus.InterprocessEvent
Interprocess events must extend EventBus.InterprocessEvent
, have a constructor which
takes a Bundle
and implement toBundle(). This allows us to serialize events to be sent
across processes.
Caveats:
WeakReference
to the publisher to prevent memory leaks, so
there must be another strong reference to the publisher for it to not get garbage-collected and
continue receiving events.
Class
. This
is only done once per class type, but if possible, it is best to pre-register an instance of
that class beforehand or when idle.
Future optimizations:
Modifier and Type | Class and Description |
---|---|
static class |
EventBus.AnimatedEvent
An event that represents an animated state change, which allows subscribers to coordinate
callbacks which happen after the animation has taken place.
|
static class |
EventBus.Event
An event super class that allows us to track internal event state across subscriber
invocations.
|
static class |
EventBus.InterprocessEvent
An inter-process event super class that allows us to track user state across subscriber
invocations.
|
static class |
EventBus.ReusableEvent
An event that can be reusable, only used for situations where we want to reduce memory
allocations when events are sent frequently (ie. on scroll).
|
BroadcastReceiver.PendingResult
Modifier and Type | Method and Description |
---|---|
void |
dump(String prefix,
PrintWriter writer) |
String |
dumpInternal(String prefix) |
static EventBus |
getDefault() |
void |
onReceive(Context context,
Intent intent)
Receiver for interprocess events.
|
void |
post(EventBus.Event event)
Post a message to the subscribers of the given event type.
|
void |
post(EventBus.InterprocessEvent event)
Deprecated.
|
void |
postInterprocess(Context context,
EventBus.InterprocessEvent event)
Posts an interprocess event.
|
void |
register(Object subscriber)
Registers a subscriber to receive events with the default priority.
|
void |
register(Object subscriber,
int priority)
Registers a subscriber to receive events with the given priority.
|
void |
registerInterprocessAsCurrentUser(Context context,
Object subscriber)
Explicitly registers a subscriber to receive interprocess events with the default priority.
|
void |
registerInterprocessAsCurrentUser(Context context,
Object subscriber,
int priority)
Registers a subscriber to receive interprocess events with the given priority.
|
void |
send(EventBus.Event event)
Sends an event to the subscribers of the given event type immediately.
|
void |
send(EventBus.InterprocessEvent event)
Deprecated.
|
void |
sendOntoMainThread(EventBus.Event event)
If this method is called from the main thread, it will be handled directly.
|
void |
unregister(Object subscriber)
Remove all EventHandlers pointing to the specified subscriber.
|
void |
unregisterInterprocess(Context context,
Object subscriber)
Explicit unregistration for interprocess event subscribers.
|
abortBroadcast, clearAbortBroadcast, getAbortBroadcast, getDebugUnregister, getPendingResult, getResultCode, getResultData, getResultExtras, getSendingUserId, goAsync, isInitialStickyBroadcast, isOrderedBroadcast, peekService, setDebugUnregister, setOrderedHint, setPendingResult, setResult, setResultCode, setResultData, setResultExtras
public static EventBus getDefault()
public void register(Object subscriber)
subscriber
- the subscriber to handle events. If this is the first instance of the
subscriber's class type that has been registered, the class's methods will
be scanned for appropriate event handler methods.public void register(Object subscriber, int priority)
subscriber
- the subscriber to handle events. If this is the first instance of the
subscriber's class type that has been registered, the class's methods will
be scanned for appropriate event handler methods.priority
- the priority that this subscriber will receive events relative to other
subscriberspublic void registerInterprocessAsCurrentUser(Context context, Object subscriber)
subscriber
- the subscriber to handle events. If this is the first instance of the
subscriber's class type that has been registered, the class's methods will
be scanned for appropriate event handler methods.public void registerInterprocessAsCurrentUser(Context context, Object subscriber, int priority)
subscriber
- the subscriber to handle events. If this is the first instance of the
subscriber's class type that has been registered, the class's methods will
be scanned for appropriate event handler methods.priority
- the priority that this subscriber will receive events relative to other
subscriberspublic void unregister(Object subscriber)
public void unregisterInterprocess(Context context, Object subscriber)
public void send(EventBus.Event event)
public void post(EventBus.Event event)
public void sendOntoMainThread(EventBus.Event event)
@Deprecated public void post(EventBus.InterprocessEvent event)
@Deprecated public void send(EventBus.InterprocessEvent event)
public void postInterprocess(Context context, EventBus.InterprocessEvent event)
public void onReceive(Context context, Intent intent)
onReceive
in class BroadcastReceiver
context
- The Context in which the receiver is running.intent
- The Intent being received.public void dump(String prefix, PrintWriter writer)