
implementation 'org.greenrobot:eventbus:3.1.1'@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EventBus.getDefault().register(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
public class MessageEvent {
public String name;
public String password;
public MessageEvent(String name, String password) {
this.name = name;
this.password = password;
}
}
EventBus.getDefault().post(new MessageEvent("dahaige","123456"));
@Subscribe(threadMode = ThreadMode.MainThread)//Doomed thread model
public void MessageEventBus(MessageEvent event){
tvData.setText(event.name);
tvTitle.setText(event.password);
}
EventBus.getDefault().cancelEventDelivery(event);//Subscribers with high priority can terminate the event and pass it down
public class StickyEvent {
public String msg;
public StickyEvent(String msg) {
this.msg = msg;
}
}
EventBus.getDefault().postSticky(new StickyEvent("I am a sticky event"));
@Subscribe(threadMode = ThreadMode.MainThread,sticky = true)//sticky = true, is to start the sticky event
public void StickyEventBus(StickyEvent event){
tvResult.setText(event.msg);
}
EventBus.getDefault().register(CActivity.this);
EventBus.getDefault().removeStickyEvent(new StickyEvent(tvResult.getText().toString()));//Remove specific sticky events
EventBus.getDefault().removeAllStickyEvents();//Remove all sticky events
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?...
What is the event bus? It is an implementation of the publish-subscribe model. It is a centralized event processing mechanism that allows different components to communicate with each other without in...
In order to simplify and more high-quality communication between Activity, Fragment, Thread and Service, etc., while solving the high coupling between components and still continue to communicate effi...
EventBus is a publish/subscribe event bus optimized for Android. The main function is to replace Intent, Handler, BroadCast are passed between Fragment, Activity, Service, and threads Message. The adv...
Overview What is EventBus? Introduction to the internal roles of EventBus Configuration Actual case Overview In the previous traditional Android development, we tended to implement Intent, Handler, Br...
I. Overview Android components, communication between threads, can be implemented with Handler, BroadcastReceiver, callback, etc., but the implementation of these methods is a cumbersome. Eventbus can...
In Vue, the data transmission between components is generally passed on the parent -child component, but in the actual application of the project, there is also no relationship of components that need...
The core concept of many modern JavaScript frameworks and libraries is the ability to encapsulate data and UI in modular, reusable components. This allows developers to avoid writing a lot of duplicat...
Reference article: The core concept of many modern JavaScript frameworks and libraries is the ability to encapsulate data and UI in modular, reusable components. This allows developers to avoid writin...
ionic4-EventBus (event bus) surroundings Welcome to visit me:Ionic4 column Foreword: AngularJs, we can use b r o a d c a s t 、 broadcast、 broadcast、On to send out, listen to the broadcast, to achieve ...