0

I'm trying to pass an event with Otto Event Bus from the SplashActivity to LoginActivity which is 2 o 3 activities later with:

SplashActivity

@Override
    public void onStart() {
        super.onStart();
        BusProvider.getInstance().register(this);

        if(UtilityManager.isAppOpenedFromNotificationTap(this)){
            BusProvider.getInstance().post(new AppOpenedFromNotificationClickEvent());
        }
        
    }

    @Override
    public void onStop() {
        super.onStop();
        BusProvider.getInstance().unregister(this);
    }

And I am getting value with:

LoginActivity

@Override
    public void onStart() {
        super.onStart();
        BusProvider.getInstance().register(this);

    }

    @Override
    public void onStop() {
        super.onStop();
        BusProvider.getInstance().unregister(this);
    }

@Subscribe
    public void onAppOpenedFromNotificationEvent(AppOpenedFromNotificationClickEvent event) {
        Log.e("LOGIN", "ARRIVED" );
        appOpenedFromNotification = true;
    }

These Log that I put inside subscribe is never showed. What the problem?

Víctor Martín
  • 3,352
  • 7
  • 48
  • 94
  • 2
    Is it possible the LoginActivity hasn't been created (and started) when the SplashActivity sends the event; in this case the LoginActivity hasn't registered yet and therefore never receives event. –  Oct 28 '20 at 16:02
  • It's probably, I need to rethink the flow. – Víctor Martín Oct 28 '20 at 16:53
  • In case you want to handle the event at a later time or being ready to be picked up when a component is ready in your case the login activity, you can create a sticky event which should suffice https://greenrobot.org/eventbus/documentation/configuration/sticky-events/ – noev Oct 28 '20 at 21:21

1 Answers1

1

Use a sticky event

for sending

EventBus.getDefault().postSticky(new MessageEvent("Hello everyone!"));

for receiving

// UI updates must run on MainThread
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEvent(MessageEvent event) {   
    textField.setText(event.message);
}
noev
  • 980
  • 8
  • 26