github EventBus
https://github.com/greenrobot/EventBus
其描述为:
Event bus for Android and Java that simplifies communication between Activities, Fragments, Threads, Services, etc. Less code, better quality
decouples event senders and receivers
performs well with Activities, Fragments, and background threads
avoids complex and error-prone dependencies and life cycle issues
makes your code simpler
EventBus使用具体方法为:
(1)在build.gradle添加库依赖
dependencies {
implementation 'org.greenrobot:eventbus:3.1.1'
}
(2)Define events:
public static class MessageEvent { /* Additional fields if needed */ }
(3)Prepare subscribers: Declare and annotate your subscribing method, optionally specify a thread mode:
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {/* Do something */};
Register and unregister your subscriber. For example on Android, activities and fragments should usually register according to their life cycle:
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
(4)Post events:
EventBus.getDefault().post(new MessageEvent());
EventBus3.0有四种线程模型,分别是:
粘性事件:
所谓粘性事件,就是在发送事件之后再订阅该事件也能收到该事件。请注意这里与普通事件的区别,普通事件是先注册在绑定。
比如在项目中有这样的需求,在FirstActivity发送事件,到SecondActivity中做事件的处理。如果是使通过EventBus.getDefault.post(xx)发出的,在SecondActivity是接收不到消息的。 主要原因是SecondActivit用于接收消息的EventBus还未完成注册,也就是发布者发了消息,但订阅者还未产生。
发送粘性事件:
EventBus.getDefault().postSticky(messageEvent);
处理粘性事件:
@Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
public void XXX(MessageEvent messageEvent) {
...
}
主要实现功能是,从第二个界面发送一个消息回第一个界面,并在第一个界面的TextView中来显示此消息.
实现逻辑:
(1)在build.gradle添加库依赖
dependencies {
implementation 'org.greenrobot:eventbus:3.1.1'
}
(2)定义传递消息
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;
}
}
(3)第一个界面实现逻辑:
public class EventBusDemoMainActivity extends AppCompatActivity {
private TextView textView;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_bus_demo_main);
init();
}
private void init() {
EventBus.getDefault().register(this);
textView = (TextView) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onDoClick();
}
});
}
private void onDoClick() {
Intent intent=new Intent(EventBusDemoMainActivity.this,SecondActivity.class);
startActivity(intent);
}
@Override
protected void onDestroy() {
super.onDestroy();
if(EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().unregister(this);
}
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void Event(MessageEvent messageEvent) {
textView.setText(messageEvent.getMessage());
}
}
第二个界面实现逻辑:
public class SecondActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
init();
}
private void init() {
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EventBus.getDefault().post(new MessageEvent("我是使用EventBus发送的第一个消息噢,亲!"));
finish();
}
});
}
}
1.EventBus 3.0使用详解
https://www.jianshu.com/p/f9ae5691e1bb
2.Android EventBus 的使用
https://www.jianshu.com/p/e7d5c7bda783