2016-09-15 29 views
4

我已经有了这个问题,因为我切换到EventBus(同样会发生在任何总线库)凡凡我想执行一个动作时,视图没有准备好,那么我会得到的错误公交车没有登记;每当视图尚未启动时使用总线?

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); 
} 
+1

你可能想看看 –

+0

难道我粘的事件必须手动处理这些事件或者是我注册后自动发送?我应该如何处理这种情况? – Jaythaking

+0

您不需要在巴士上注册即可在巴士上发布信息。您是否正在使用事件总线在单个活动中路由消息?如果是的话,为什么? – CommonsWare

回答

1

这是同样的问题,为什么ÿ如果您试图在onActivityResult中显示对话框片段,您会收到IllegalStateException。简而言之,它在用户界面恢复正常之前运行。

简单的解决方案:

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.

+0

但是我不明白为什么事件总线与视图具有相同的生命周期?如果视图尚未准备好,为什么我无法与其他服务进行通信? – Jaythaking

相关问题