compile 'org.greenrobot:eventbus:3.0.0'
注册:
EventBus.getDefault().register(this);
解除: @Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
用法:新建
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;
}
}
MainActivity定义处理事件方法@Subscribe(threadMode = ThreadMode.MAIN)一定要写方法名随意
就收信息并显示到bt2上
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventTest(MessageEvent message) {
bt2.setText(message.getMessage());
}
MainActivity跳转到EventBusTestActivity
EventBusTestActivity,点击bt2将信息传递回MainActivity,返回到MainActivity后bt1显示“测试成功”
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EventBus.getDefault().post(new MessageEvent("测试成功"));
finish();
}
});