当前位置:网站首页>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
边栏推荐
- RTP 发送 和接收 h265
- Sanzi chess (player + computer)
- Score addition and subtraction of force deduction and brushing questions (one question per day 7/27)
- 3.1 common neural network layer (I) image correlation layer
- Swing V2: towards a larger model with larger capacity and higher resolution
- (2022杭电多校三)1002-Boss Rush(状压DP+二分)
- 后缀自动机(sam)板子 from jly
- Typescript learning (I)
- 实例搭建Flask服务(简易版)
- Design of smoke temperature, humidity and formaldehyde monitoring based on single chip microcomputer
猜你喜欢
![[technology 1]](/img/eb/63baf1ae3931a156a0a5b377a9b7d1.jpg)
[technology 1]

Violence recursion to dynamic programming 01 (robot movement)

Producer consumer model of concurrent model
![Leetcode 1331 array sequence number conversion [map] the leetcode path of heroding](/img/be/d429d0c437dc5ed7cb4448e223a83a.png)
Leetcode 1331 array sequence number conversion [map] the leetcode path of heroding

【C】 Array

Rdkit: introduce smiles code, smart code and Morgan fingerprint (ECFP)

实例搭建Flask服务(简易版)

exness:鸽派决议帮助黄金反弹,焦点转向美国GDP

3D advanced renderer: artlandis studio 2021.2 Chinese version

Arm architecture and neural network
随机推荐
web-uploader不能多文件上传
The Federal Reserve raised interest rates again, Powell "let go of doves" at 75 basis points, and US stocks reveled
C obtains JSON format data asynchronously from the web address
Rongyun IM & RTC capabilities on new sites
Set functions in kotlin
Several methods of converting object to string
Idea configuration web container and war packaging
RTP 发送 和接收 h265
Simple code implementation of K-means clustering
exness:鸽派决议帮助黄金反弹,焦点转向美国GDP
如何判定是stun协议
Notes on letter symbol marking of papers
NXP i.mx8mp-deepviewrt
Suffix automata (SAM) board from Jly
Alibaba Sentinel - workflow and principle analysis
Summary of SAP localized content in China
容斥原理
Numpy acceleration -- > cupy installation
Use of leak scanning (vulnerability scanning) tool burpsuite or burp Suite (with installation and installation package download of burpsuite+1.7.26)
What is eplato cast by Plato farm on elephant swap? Why is there a high premium?