public interface Bus
The Bus allows publish-subscribe-style communication between components without requiring the components to explicitly register with one another (and thus be aware of each other). It is designed exclusively to replace traditional Android in-process event distribution using explicit registration or listeners. It is not a general-purpose publish-subscribe system, nor is it intended for interprocess communication.
Subscribe annotation;register(Object) method.post(Object) method.
The Bus instance will determine the type of event and route it to all registered listeners.
Events are routed to all subscribers registered for exactly this type of event. Event's class hierarchy is ignored.
When post is called, all registered subscribers for an event are run in sequence
synchronously, so subscribers should be reasonably quick. If an event may trigger an extended
process (such as a database load), spawn a thread or queue it for later.
You are allowed to post a new event while already handling an event inside a subscriber method. In this case the bus will finalize dispatching of current event first and then a new event will be dispatched.
Handlers should not, in general, throw. If they do, the Bus will wrap the exception and re-throw it.
This class is not safe for concurrent use. It must be called from a single thread.
| Modifier and Type | Method and Description |
|---|---|
void |
cancelDelayed(java.lang.Class<?> eventClass)
Removes a pending event of given type from delivery queue.
|
boolean |
hasRegistered(java.lang.Object object)
Checks whether given object is currently registered in the bus.
|
void |
post(java.lang.Object event)
Posts an event to all registered handlers.
|
void |
postDelayed(java.lang.Object event,
long delayMillis)
Causes the event to be posted to the bus after the specified amount of time elapses.
|
void |
register(java.lang.Object object)
Registers all handler methods on
object to receive events and producer methods to provide events. |
void |
unregister(java.lang.Object object)
Unregisters all producer and handler methods on a registered
object. |
void register(java.lang.Object object)
object to receive events and producer methods to provide events.
If any subscribers are registering for types which already have a producer they will be called immediately with the result of calling that producer.
If any producers are registering for types which already have subscribers, each subscriber will be called with the value from the result of calling the producer.
object - object whose handler methods should be registered.java.lang.NullPointerException - if the object is null.void unregister(java.lang.Object object)
object.object - object whose producer and handler methods should be unregistered.java.lang.IllegalArgumentException - if the object was not previously registered.java.lang.NullPointerException - if the object is null.void post(java.lang.Object event)
event - event to post.java.lang.NullPointerException - if the event is null.boolean hasRegistered(java.lang.Object object)
In most cases, when you (un)register objects inside standard
onStart() and onStop() lifecycle callbacks,
you don't need this method at all. But in some more trickier
cases, when you (un)register objects depending on some other
conditions, this method can be very helpful.
object - the object to checktrue if object is registered or
false otherwisevoid postDelayed(java.lang.Object event,
long delayMillis)
Note that the time-base is SystemClock.uptimeMillis(). This means
the time spent in deep sleep will add an additional delay to execution.
event - event to be posteddelayMillis - delay (in milliseconds) until the event will be executedvoid cancelDelayed(java.lang.Class<?> eventClass)
eventClass - event type of event to be cancelled