【问题标题】:Using bus whenever the view isn't started yet?在视图尚未开始时使用总线?
【发布时间】:2016-09-15 22:45:41
【问题描述】:

自从我切换到 EventBus (任何总线库都会发生同样的情况) 后我遇到了这个问题t准备好了,然后我会得到总线未注册的错误;

E/EventBus: Could not dispatch event: class com.android.greenfield.Action to subscribing class class com.android.greenfield.GreenStore

当我想在生命周期的这些部分触发一个动作时会发生这种情况:

当我拍摄图片/视频时:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {   
   actionsCreator.uploadFile(filepath, "image/jpg");
   // ... (Error here because the bus isn't yet registered)
}

或者当我收到 NFC TAG 时:

@Override
public void onNewIntent(Intent intent) {
   actionsCreator.uploadNfcTag(intent);
   // ... (Error here because the bus isn't yet registered)
}

如果我按照他们的文档中所说的正常方式或 EventBus,我应该这样 registerunregister

@Override
public void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);
}

@Override
public void onStop() {
    EventBus.getDefault().unregister(this);
    super.onStop();
}

到目前为止,我发现的唯一解决方法是注册和取消注册,当我想要执行介于 onStart()onStop() 生命周期之间的操作时...但它很脏,我感觉很糟糕

@Override
public void onNewIntent(Intent intent) {
   dispatcher.register(GreenStore);
   actionsCreator.uplaodNfcTag(intent);
   dispatcher.register(GreenStore);
}

【问题讨论】:

  • 你可能想看看粘性事件
  • 我必须手动处理这些事件,还是在我重新注册后自动交付?在我的情况下我应该如何管理它?
  • 您无需在公交车上注册即可在公交车上发布消息。您是否使用事件总线在单个活动中路由消息?如果是,为什么?
  • 不,我有一个调度员,它链接到公共汽车并将事件发布到另一个班级
  • 我更新了我的问题以更忠实于我的代码...

标签: android event-bus greenrobot-eventbus


【解决方案1】:

如果您尝试在onActivityResult 中显示对话框片段,这与您收到IllegalStateException 的问题相同。简单地说,它会在 UI 恢复之前运行。

简单的解决方案:

1.)

Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
    @Override
    public void run() {
         actionsCreator.uploadFile(filepath, "image/jpg");
    }
});

2.) while the bus is paused (this is something you'd handle manually), you should queue up the events, and then when it's unpaused, execute them.

【讨论】:

  • 但我不明白为什么事件总线与视图具有相同的生命周期?当视图尚未准备好时,为什么我无法与其他服务通信?
猜你喜欢
  • 2015-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多