gpt4 book ai didi

android - 在 View 尚未开始时使用总线?

转载 作者:太空宇宙 更新时间:2023-11-03 10:38:03 26 4
gpt4 key购买 nike

自从我切换到 EventBus (任何总线库都会发生同样的情况) 每当我想在 View 不存在时执行操作时,我就遇到了这个问题没有准备好,然后我会得到总线未注册的错误;

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

当我想在生命周期的那些部分触发一个 Action 时发生:

当我拍摄照片/视频时:

@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,我应该以这种方式注册注销:

@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);
}

最佳答案

如果您尝试在 onActivityResult 中显示对话框 fragment ,这与您得到 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.

关于android - 在 View 尚未开始时使用总线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39521433/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com