First of all, let's talk about the event bus, its role: in order to simplify and higher quality communication between Activity, Fragment, Thread and Service, etc., to solve the high coupling between components and still carry out efficient communication.
EventBus is a publish-subscribe event bus optimized for Android, which simplifies the communication between various components in the application and background threads.
Its advantages are low overhead, more elegant code, and decoupling the sender and receiver.
(1) Customize an event class
public class MessageEvent{
...
}
(2) Register the event where you need to subscribe to the event
EventBus.getDefault().register(this);
(3) Send event
EventBus.getDefault().post(messageEvent);
(4) Handling events
@Subscribe(threadMode = ThreadMode.MAIN)
public void XXX(MessageEvent messageEvent){
...
}
(5) Cancel the registration event
EventBus.getDefault().unregister(this);
compile 'org.greenrobot:eventbus:3.0.0'
public class MessageEvent {
private String message;
public MessageEvent(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
public class MainActivity extends AppCompatActivity {
private TextView tv_message;
private Button btn_subscription, btn_message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_message = (TextView) findViewById(R.id.tv_message);
tv_message.setText("MainActivity");
btn_subscription = (Button) findViewById(R.id.btn_subscription);
btn_subscription.setText("Registration issue");
btn_message = (Button) findViewById(R.id.btn_message);
btn_message.setText("Jump to SecondActivity");
btn_subscription.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Registration issue
EventBus.getDefault().register(MainActivity.this);
}
});
btn_message.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
//Cancel registration event
EventBus.getDefault().unregister(MainActivity.class);
}
}
Customize methods in MainActivity to handle events.
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMoonEvent(MessageEvent messageBean) {
//Show the received message
tv_message.setText(messageBean.getMessage());
}
Create SecondActivity to publish a message.
public class SecondActivity extends AppCompatActivity {
private TextView tv_message;
private Button btn_message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
tv_message = (TextView) findViewById(R.id.tv_message);
tv_message.setText("SecondActivity");
btn_message = (Button) findViewById(R.id.btn_message);
btn_message.setText("Send event");
btn_message.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().post(new MessageEvent("Welcome to learn EventBus"));
finish();
}
});
}
}
The running program is as follows, when we click the "register event" button in MainActivity to register the event, and then click the "jump to SECONDACTIVITY" button to jump to SecondActivity. Next, we click "Send Event". At this time, finishSecondActivity displays MainActivity, and TextView displays "Welcome to learn EventBus". In this way, MainActivity successfully received the event sent in SecondActivity. As shown:
EventBus sticky event means that you can receive the event even if you subscribe to the event after sending the event. Modify the code as follows:
@Subscribe(threadMode = ThreadMode.POSTING,sticky = true)
public void onMoonStickyEvent(MessageEvent messageEvent){
tv_message.setText(messageEvent.getMessage());
}
Define Button in SecondActivity to send sticky events.
btn_subscription.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().postSticky(new MessageEvent("Sticky events"));
finish();
}
});
Now we run the program, without clicking "register event" in our MainActivity, jump directly to SecondActivity to send sticky events. At this time back to our MainActivity interface.
————————————>
Next, we click on the registration event, you will find that the TextView changes to display "sticky events", and you're done.
-keepattributes *Annotation*
-keepclassmembers class ** {
@org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
# Only required if you use AsyncExecutor
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
<init>(java.lang.Throwable);
}
Children's shoes are welcome to learn the basic use of EventBus.
For more please see:
EventBus github address:https://github.com/greenrobot/EventBus
EventBus official website:http://greenrobot.org/eventbus/
Eventbus 3.0 Usage Analysis Foreword: Why do Eventbus? 1.Eventbus basically use 2.Eventbus viscous event Foreword: Why do Eventbus? Eventbus acts as an optimized publish / subscription event bus in th...
2019 Unicorn Enterprise Heavy Glour Recruitment Python Engineer Standard >>> 1, download Eventbus class library Source code:https://github.com/greenrobot/EventBus 2, basically use (1) Customi...
Vuex to use 1. Introduction and Installation 2. Configure 3. Registration 4. In the assembly 1. Introduction and Installation EventBus: Also known as the event bus. EventBus can be used in the Vue as ...
1. Guide package 2. Add confusion The obfuscation rules are as follows 3. The first activity 4. The second activity 5. Simple functions are achieved...
Vue components are very common with parent-child component communication and brother component communication. The communication between parent and child components is very simple. The parent component...
1. Overview EventBus definition: it is a publish/subscribe event bus. In this way, it should contain 4 components: publisher, subscriber, event, and bus. So what is the relationship between these four...
purpose of usage Communication within the component is also transmitted data Scope of application The brothers component passes the value, the grandchildren component pass the value, and the component...
1 Introduction Once, layer after layer of business logic made me feel overwhelmed, one after another callback makes you dizzy, one parameter after another makes you confused. EventBus, a framework tha...
Introduction GitHub:https://github.com/greenrobot/EventBus Let's talk about what the EventBus thread bus does, the environment, its advantages and disadvantages. What are you doing? In a word, simple ...
Android development framework EventBus Series event bus Introduction GitHub:https://github.com/greenrobot/EventBus EventBus to talk threads bus is doing, environment, advantages, disadvantages. Doing?...