Simple use of EventBus Android EventBus and eventbus Android EventBus
The basic steps are as follows. Click this link to view the example and introduction.
- Define the event type:
'Public class MyEvent {}'
- Define event handling methods:
'Public void oneventmainthread'
- Subscriber registration:
'Eventbus. getDefault (). register (this )'
- Send event:
'Eventbus. getDefault (). post (new MyEvent ())'
I. Implementation
** EventBus ** is easy to use, but if you do not know how to use it, you will never know how to use it, so we need to study its implementation and Read the fucking Code. In fact, it is mainly a 'eventbus' class. When you look at the Code, you need to understand several concepts and members. After understanding these concepts, you can understand the implementation well.
- EventType: the parameter in the onEvent \ * function, indicating the Event Type
- Subscriber: subscription source, that is, the object registered by register, which contains the onEvent \ * Function
- SubscribMethod: a specific onEvent \ * Method in 'subscriber '. The internal Member contains a method Member of the 'method' type to indicate this onEvent \ * Method, A 'threadmode' member ThreadMode indicates the event processing thread and A 'Class <?> 'Eventtype member of 'type' indicates the event type 'eventtype '.
- Subscriber, indicating a Subscription object, including the Subscription source 'subscriber ', a specific method in the Subscription source 'subscribmethod', the priority of this Subscription 'priopity'
After learning about the above concepts, You can see several important members in 'eventbus '.
// EventType-> List <subpartition>, priving between events and Subscription objects privatefinal Map <Class <?>, CopyOnWriteArrayList <subscri>> subscriptionsByEventType; // Subscriber-> List <EventType>: ing privatefinal Map <Object, list <Class <? >>> TypesBySubscriber; // stickEvent event. The privatefinal Map <Class <?>, Object> stickyEvents; // EventType-> List <? Extends EventType>, which maps events to its parent event list. Cache all the parent classes of A Class. privatestaticfinal Map <Class <?>, List <Class <? >>> EventTypesCache = new HashMap <Class <?>, List <Class <? >>> ();
Ii. Registration event: Register
Use 'eventbus. getDefault (). the register 'method can be used to register with 'eventbus' to subscribe to events. 'register 'has many reload forms, but most of them are marked as 'deprecated', so it is better not to subscribe to events, as mentioned above, all event handling methods start with * onEvent * and can be modified using the register method. However, the corresponding method is discarded and should not be used, use the default * onEvent *. In addition to the discarded register method, there are also four 'register 'Methods ** public **.
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);}
We can see that the four methods 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); }}
It can be seen that:
The first parameter is the subscription source, and the second parameter uses the specified method name convention. The default parameter starts with * onEvent *, which means that the default parameter can be modified, but as mentioned above, the method has been abandoned and it is best not to use it. The third parameter indicates whether it is * Sticky Event *, and the second parameter indicates the priority.
In the above method, a class named 'subscribermethodfinder 'is used to locate a 'subscribermethod' list through its 'findsubscribermethods' method, as we have known before, 'subscribermethod' indicates an onEvent \ * method in the Subcriber. We can see that the role of the 'subscribermethodfinder 'class is to find all the methods in the Subscriber (that is, the default onEvent) method. Each method found is represented as a 'subscribermethod' object.
'Subscribermethodfinder 'will not be analyzed, but there are two points to know:
- All event processing methods ** must be of the 'public void' type **, and only one parameter indicates * EventType *.
- 'Findsubscribermethods 'not only looks for the event handling methods in * Subscriber *, but also finds the event handling methods in all the base classes in its inheritance system **.
After all the event handling methods in * Subscriber * are found, the 'subscribe' method is called for registration for each found method (represented as the 'subscribermethod' object. The 'subscribe' method does three things:
- Store the 'subscribermethod' object in 'subscriptionsbyeventtype' Based on the * EventType * type in 'subscribermethod. Creates a ing between * EventType * and * subscribe *. Each event can have multiple subscribers.
- Store 'eventtype' in 'typesbysubscriber 'based on 'subscriber' and create a ing between * Subscriber * and * EventType *. Each Subscriber can subscribe to multiple events.
- If it is a * Sticky * type subscriber, send the last saved event directly to it (if any ).
Through the subing between * Subscriber * and * EventType *, we can easily cancel the event receiving by a Subscriber and use * EventType * to map * Sucscribtion, you can easily send events to every subscriber.
Next, we will analyze each event.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.