Use bus whenever view is not already running?

I have this problem since I switched to EventBus (the same thing will happen with any bus library), where whenever I want to perform an action when the view is not ready, I get an error that the bus is not registered;

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

This happens when I want to run Action in those parts of the life cycle:

When I take Image / Video :

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

Or here when I get the NAG TAG :

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

If I follow the normal path or EventBus , as their document says, I should registeralso unregisteras follows:

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

, IllegalStateException, onActivityResult. , .

:

1.)

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

2.) ​​( , ), , , , .

+1

Source: https://habr.com/ru/post/1654752/


All Articles