当前位置:网站首页>Simple use of eventbus
Simple use of eventbus
2022-07-29 03:33:00 【Young people in Baima town】
summary
EventBus It's a Android Event publishing / Subscription framework , Simplify... By decoupling publishers and subscribers Android events , The event here can be understood as a message . Event passing can be used both for Android Communication between the four components , It can also be used for communication between asynchronous threads and mainthreads .
Traditional event delivery methods include :Handler、BroadcastReceiver、Interface Callback , by comparison EventBus The point of is that the code is concise , Easy to use , And publish the event and Subscriptions are fully decoupled
1. The first step is to add a class library , //eventbus Transmit information implementation 'org.greenrobot:eventbus:3.1.1'
2. Popular said
To build a A class ( Received ),
onCreate Register inside
EventBus.getDefault().register(this);
onDestroy Internal deregistration
EventBus.getDefault().unregister(this);
Creating subscription methods , When an event is received , This method is called
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(Event messageEvent) {
Log.e("TAG", "------------>"+ messageEvent.getMessage());
Toast.makeText(MainActivity.this, messageEvent.getMessage(), Toast.LENGTH_SHORT).show();
}
Create an entity class
public class Event {
private String message;
public Event(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
stay B Class ,A Class onEvent Method
B Class to send message events
// Send events
EventBus.getDefault().post(new Event("Just do it"));
build.gradle Add //eventbus Transmit information
implementation 'org.greenrobot:eventbus:3.1.1'
Complete code
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(this);
// Registration events
EventBus.getDefault().register(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button:// Click into another class , Send a message event in another class
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
break;
}
}
// Subscription method , When an event is received , This method is called
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(Event messageEvent) {
Log.e("TAG", "------------>"+ messageEvent.getMessage());
Toast.makeText(MainActivity.this, messageEvent.getMessage(), Toast.LENGTH_SHORT).show();
}
// Unregister
@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
Another class
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
Button login_image_QQlogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login_image_QQlogin = findViewById(R.id.login_image_QQlogin);
login_image_QQlogin.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.login_image_QQlogin :
new Thread(new Runnable() {
@Override
public void run() {
// Send events
EventBus.getDefault().post(new Event("Just do it"));
}
}).start();
break;
}
}
}
Entity class
public class Event {
private String message;
public Event(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
Register and unregister in which class you receive events , Rewrite the receiving method , Deepen understanding EventBus It is suggested to go to the official website to see the source code
边栏推荐
- Producer consumer model of concurrent model
- How to realize shortcut keys for interface scaling by vscade
- for_each用法示例
- HDU multi School Game 2 1011 DOS card
- 通过递归实现多级联动
- Machine learning based on deepchem
- Rdkit II: use rdkit screening to screen 2D pharmacophores of chemical small molecules
- Learn exkmp again (exkmp template)
- Realize multi-level linkage through recursion
- Anti vulnerability · benefit from uncertainty --- management?
猜你喜欢
3D advanced renderer: artlandis studio 2021.2 Chinese version
mysql的timestamp存在的时区问题怎么解决
Machine learning based on deepchem
Machine learning [numpy]
Rdkit II: use rdkit screening to screen 2D pharmacophores of chemical small molecules
[technology 1]
How does DataGrid export and recover the entire database data, using a single SQL file
Use of leak scanning (vulnerability scanning) tool burpsuite or burp Suite (with installation and installation package download of burpsuite+1.7.26)
Rdkit: introduce smiles code, smart code and Morgan fingerprint (ECFP)
ROS - create workspace
随机推荐
Whole process record of yolov3 target detection
Implement Lmax disruptor queue from scratch (VI) analysis of the principle of disruptor solving pseudo sharing and consumers' elegant stopping
Pp-yoloe details
During the year, the first "three consecutive falls" of No. 95 gasoline returned to the "8 Yuan era"“
一种简单通用的获取函数栈空间大小的方法
How to solve the time zone problem in MySQL timestamp
腾讯云使用pem登录
Simple understanding of CDN, SDN and QoS
CUDA GDB prompt: /tmp/tmpxft**** cudafe1.stub. c: No such file or directory.
KNN method predicts pregnancy, KNN principle simple code
How to realize multi line annotation in MATLAB
3.1 common neural network layer (I) image correlation layer
Numpy acceleration -- > cupy installation
A case of gradually analyzing the splitting of classes -- colorful ball collisions
MOS管 —— 快速复苏应用笔记(贰)[参数与应用]
Asynchronous callback future mode of concurrent mode
Multi level wavelet CNN for image restoration
Introduction and advanced MySQL (XIV)
Bingbing learning notes: operator overloading -- implementation of date class
Does domestic ERP have a chance to beat sap?