Android:绿色机器人事件总线:在单个 post 上接收到多个事件
Android :Green robot eventbus : Multiple events received on single post
我在 Android
中使用绿色机器人事件总线
我正在使用 EventBus.getDefault().post 和 onStop 调用所有事件我正在调用 EventBus.getDefault().unregister(this);在我的 Activity 中。但是,一旦我按下并重新打开应用程序,在单个事件 post 上,会收到多个 onEvent()。还有其他人遇到过这个问题吗?
@Override
protected void onStart() {
super.onStart();
getBus().register(this);
}
@Override
protected void onPause() {
getBus().unregister(this);
super.onPause();
}
@Override
protected void onStop() {
getBus().unregister(this);
super.onStop();
}
protected EventBus getBus() {
return EventBus.getDefault();
}
我想通了。每次应用程序从后台返回时,都会再次调用注册函数。与我的错误假设相反,绿色机器人没有管理重复项,我需要在注册之前添加一个检查。所以这就是我的最终代码的样子。
mBus = EventBus.getDefault();
void registerAndCheck(Object helper)
{
if(!mBus.isRegistered(helper))
{
mBus.register(helper);
}
}
mFileHelper = new FilesHelper();
registerAndCheck(mFileHelper);
希望对大家有所帮助。
我在 Android
中使用绿色机器人事件总线我正在使用 EventBus.getDefault().post 和 onStop 调用所有事件我正在调用 EventBus.getDefault().unregister(this);在我的 Activity 中。但是,一旦我按下并重新打开应用程序,在单个事件 post 上,会收到多个 onEvent()。还有其他人遇到过这个问题吗?
@Override
protected void onStart() {
super.onStart();
getBus().register(this);
}
@Override
protected void onPause() {
getBus().unregister(this);
super.onPause();
}
@Override
protected void onStop() {
getBus().unregister(this);
super.onStop();
}
protected EventBus getBus() {
return EventBus.getDefault();
}
我想通了。每次应用程序从后台返回时,都会再次调用注册函数。与我的错误假设相反,绿色机器人没有管理重复项,我需要在注册之前添加一个检查。所以这就是我的最终代码的样子。
mBus = EventBus.getDefault();
void registerAndCheck(Object helper)
{
if(!mBus.isRegistered(helper))
{
mBus.register(helper);
}
}
mFileHelper = new FilesHelper();
registerAndCheck(mFileHelper);
希望对大家有所帮助。