-
1、很多人都在 onStop里面post过去,可能会影响内存泄漏,那样可能要对导致内存泄漏的对象做处理。
EventBus.getDefault().post(new ReplyDetailEvent()); }
2、通过EventBus的黏性事件处理
public void btnSend(View view) {
EventBus.getDefault().postSticky(new ObjEvent(etName.getText().toString(), etAge.getText().toString()));
startActivity(new Intent(OneActivity.this, TwoActivity.class));
}
在TwoActivity上接收赋值并使用
/**
* @desc 接收第一个界面传递过来的数据
* @author lzPeng
* @time 2018/4/27 20:15
*/
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
//注册绑定事件即可接收相关数据
EventBus.getDefault().register(this);
//处理相关数据
if (null != mObjEvent) {
((TextView) findViewById(R.id.tv_data)).setText("name:" + mObjEvent.getName() + "\n\n" + "age:" + mObjEvent.getAge());
}
}
@Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
public void onDataEvent(ObjEvent objEvent) {
if (null != objEvent) {
//赋值
this.mObjEvent = objEvent;
}
}
protected void onDestroy() {
//移除全部粘性事件
EventBus.getDefault().removeAllStickyEvents();
//解绑事件
EventBus.getDefault().unregister(this);
super.onDestroy();
}