This is the fifth day of my participation in the August More text Challenge. For details, see:August is more challenging
👉 About the author
As we all know, life is a long process of overcoming difficulties, reflecting and moving forward. In this process, there will be a lot of questions and thinking about life, so I decided to share my own thinking, experience and story, in order to find resonance!! Focus on Android/Unity and various game development skills, as well as various resource sharing (website, tools, materials, source code, games, etc.)
👉 Soon to learn
Learn EventBus to define events, listen for events, send events, use fast decoupling in the project, to achieve in the project development process is full of happiness.
👉 background
🙎 Xiaozhi: Xiaokong, xiaokong, where did EventBus come from?
🙈 Small space: It is a lightweight observer mode event publish/subscribe framework for Android contributed by the GreenRobot organization, which also contributed (greenDAO and ObjectBox are both frameworks for Creating and managing Android databases).
🙎 Xiaozhi: So when should I use her? 😝
🙈 Xiaokong (😎) : When we carry out project development, often need to program components, background threads and other communication, such as child threads request data to notify the UI, two activities or fragments, and so on, if the project is more and more complex, using Handler Intent data communication code, will gradually bloated and highly coupled! Using EventBus simplifies communication between components, greatly reducing these redundations and deep decoupling!
🙎 Xiaozhi (😆) : Whoa, whoa, why don’t you tell me how to use it?
👉 Practice Process
There are three elements of EventBus
- The Event Event. It can be any type.
- Subscriber Event Subscriber. Before EventBus3.0, you can check it if you are interested. After 3.0, you can take any method name for event processing, but you need to annotate @subscribe() and specify the threading model, which defaults to POSTING.
- Publisher Indicates the Publisher of the event. We can publish events in any thread. Normally, we use eventbus.getDefault () to get an EventBus Object and then call post(Object).
There are four thread modes
- POSTING (default) represents the event handler in the same thread as the POSTING thread.
- MAIN means that the event handler thread is in the MAIN thread (UI), so no time-consuming operations can be performed here.
- BACKGROUND indicates that the event handler thread is in the BACKGROUND thread and therefore cannot perform UI operations. If the thread that publishes the event is the main thread (UI thread), then the event handler will start a background thread, and if the thread that publishes the event is in the background thread, then the event handler will use that thread.
- ASYNC means that no matter which thread the event is published on, the event handler will always run on a new child thread and will not perform UI operations.
New drivers on the road
Open your App’s build.gradle and add the latest EventBus dependencies in dependencies:
Implementation 'org. Greenrobot: eventbus: 3.2.0Copy the code
Step 1: Define the event class
It is the data carrier of the communication process, used to pass data, similar to the entity class, this is just a simple one
public class MessageEvent {
}
Copy the code
Step 2: Register events
Step 3: Send the event
EventBus.getDefault().post(new MessageEvent());
Once sent, the @subscribe annotated function is called;
Looking back at the GitHub home page, it’s easy to understand
This is enough for most of our module communication needs. For the first time, feel free to drive in code.
Junior new driver Advanced intermediate new driver
First, a note:
As shown in the figure, the official registration/anti-registration is written in onStart/onStop. You should pay attention to the problem triggered by page jump and so on. Therefore, it should be written in the appropriate position according to the project situation;
More skills from EventBus:
-
Pass data in the event
-
Sticky Events are sent
-
Specifies the event receiving thread
-
Priority level for receiving events
-
Abort event transmission
-
Subscriber index
To pass the data, just define the constructor in the event class, add parameters, and add the set and get methods
Viscous event
We have been talking about sending messages and subscribers receive messages, notice that this is the first to subscribe to send, if I want to after the message is sent, the new registered subscriber can also receive the message sent before, this Sticky Event out;
For example, an Activity has multiple fragments that execute the same event, but I only initialize two fragments. After I post the event, I initialize two more fragments, and I also want to receive the message. The sticky event solves this problem. Sticky events don’t just disappear once, but stay in the system until removeStickyEvent removes them. Of course, there are two things that need to be done:
** Send sticky event, change post to postSticky
Eventbus.getdefault ().poststicky (new MessageEvent(” empty name “)); eventbus.getDefault ().poststicky (new MessageEvent(” empty name “));
** @subscribe = true ** Subscribe = true ** Subscribe = true ** Subscribe = true
Specifies the receiving thread for the event
The default value threadMode: POSTING, and sending the event in the same thread
ThreadMode list
-
Threadmode.posting: In the same thread as sending events
-
ThreadMode. MAIN: the MAIN thread
-
ThreadMode. BACKGROUND: the child thread
-
Threadmode. ASYNC: Indicates an asynchronous thread
We just said that @subscribe has a couple of properties, priority is the priority, and it has a default value of 0; Set your own number. The larger the number, the lower the priority, and the later the message will be received
Similar to ordered broadcasting, if you have subscribed events in multiple places with different priorities, the higher priority subscriber can terminate the event passing down.
Congratulations on getting through the new driver phase! Next step slowly analysis EventBus principle content!
👉 other
📢 by xiao Kong and xiao Kong in Xiao Zhi 📢. 📢 Welcome to like 👍 save 🌟 leave 📝