The basic use step is the following 4 steps, click this link to see examples and introduction.
- Define the event type:
' public class MyEvent {} '
- Define Event handling methods:
' public void Oneventmainthread '
- Registered Subscribers:
' Eventbus.getdefault (). Register (This) '
- Send event:
' Eventbus.getdefault (). Post (New MyEvent ()) '
I. Implementation
**eventbus** the use of the method is very simple, but with a thing, if you do not understand its implementation of the heart is always no end, in case of problems do not know, so or to study its implementation, definitely read the fucking Code. In fact, the main is ' Eventbus ' this class, in looking at code need to understand a few concepts and members, understand these after implementation is very good understanding.
- A parameter in the Eventtype:onevent\* function that represents the type of event
- Subscriber: The subscription source, which is the object registered by the register, contains the onevent\* function in the object
- Subscribmethod: ' Subscriber ' within a particular onevent\* method, an internal member that contains a ' method ' type method member represents this onevent\* approach, a ' threadmode ' The member Threadmode represents the processing thread of the event, and a EventType member of the ' class<?> ' type represents the type ' eventtype ' of the event.
- Subscription, which represents a subscription object that contains the feed ' subscriber ', a specific method in the feed ' subscribmethod ', the priority of this subscription ' priopity '
After understanding the above concepts, we can see several important members of ' Eventbus '.
EventType-List<subscription>, the mapping between events to subscribed objects privatefinal map<class<?> Copyonwritearraylist<subscription>> Subscriptionsbyeventtype; Subscriber-list<eventtype>, feeds the mappings of all event types to which it subscribes privatefinal Map<object, list<class<?> >> Typesbysubscriber; Stickevent event, the following will see privatefinal map<class<?> object> stickyevents; EventType-list<? Extends Eventtype>, the mapping of the event to its parent event list. That is, all the parent classes of a class are cached privatestaticfinal map<class<?> list<class<?>>> eventtypescache = new Hashmap<class<?>, list<class<?>>> ();
Two. Register Event: Register
Through the ' Eventbus.getdefault (). Register ' method you can subscribe to the event by registering with ' Eventbus ', ' register ' has many overloaded forms, but most of them are labeled ' Deprecated ', so it is still not good, Earlier said that the event processing methods are beginning with *onevent*, in fact, can be modified by the Register method, but the corresponding method was discarded, or do not use, with the default *onevent*, in addition to the obsolete register method, there are the following 4 **public* * the ' register ' method
Publicvoid Register (Object subscriber) { Register (subscriber, Defaultmethodname, false, 0);} Publicvoid Register (Object subscriber, int priority) { Register (subscriber, Defaultmethodname, false, priority);} Publicvoid Registersticky (Object subscriber) { Register (subscriber, Defaultmethodname, True, 0);} Publicvoid Registersticky (Object subscriber, int priority) { Register (subscriber, Defaultmethodname, True, priority);}
As you can see, these 4 methods all call the same method:
Privatesynchronizedvoid Register (Object subscriber, String methodName, boolean sticky, int priority) { list< subscribermethod> subscribermethods = Subscribermethodfinder.findsubscribermethods (Subscriber.getClass (), MethodName); for (Subscribermethod subscribermethod:subscribermethods) { Subscribe (subscriber, Subscribermethod, sticky, priority);} }
Thus:
The first parameter is the feed, the second parameter is used to specify the method name convention, the default is *onevent* start, said the default is actually can be modified by the parameters, but said before, the method has been discarded, it is best not to use. The third parameter indicates whether it is *sticky event*, and the 4th parameter is the priority, which is the two later.
In the above method, a class called ' Subscribermethodfinder ' was used, and a ' subscribermethod ' list was found by its ' findsubscribermethods ' method, which was previously known as ' Subscribermethod ' represents a onevent\* method within Subcriber, which can be seen as ' Subscribermethodfinder ' The function of a class is to find all methods in subscriber that begin with MethodName (that is, the default onevent), and each found method is represented as a ' Subscribermethod ' object.
' Subscribermethodfinder ' is no longer analyzed, but there are two points to know:
- All Event Handling Methods * * must be ' public void ' type *, and only one parameter represents *eventtype*.
- ' Findsubscribermethods ' not only looks for event handling methods within *subscriber*, but also finds event handling methods in all base classes in its inheritance system * *.
Once all the event-handling methods in *subscriber* are found, the ' subscribe ' method is registered for each method found (represented as the ' Subscribermethod ' object). The ' subscribe ' method has done three things:
- The ' subscribtion ' object is stored in ' subscriptionsbyeventtype ' according to the *eventtype* type in ' Subscribermethod '. Establish a mapping of *eventtype* to *subscription*, where each event can have multiple subscribers.
- The ' eventtype ' is stored in ' typesbysubscriber ' according to ' subscriber ', creating a map of *subscriber* to *eventtype*, and each Subscriber can subscribe to multiple events.
- If it is a *sticky* type of subscriber, send it the last saved event (if any) directly to it.
With the *subscriber* to *eventtype* mapping, we can easily make a subscriber cancel the receive event, through the *eventtype* to the *sucscribtion* map, It is easy to send the appropriate event to each of its subscribers.
The next step is a specific analysis of each event.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Eventbus Simple use of basic steps is the following 4 steps, click on this link to see examples and introduction. Define event Type: ' public class MyEvent {} ' defines event handling method: ' Public