Suppose our app has 3 activities: A, B and C. And only A is registered to EventBus at onStart()
and unregistered at onStop()
as explained here, and it has an event handler method: onEvent(X event){...}
which navigates the user to C upon receiving of the event X.
But see below unhappy scenario:
- User launches A.
- User starts a background operation on A.
- User navigates to B by tapping a button on A without waiting for the background operation to be completed. A will be unregistered from EventBus.
- User navigates back to A. A will be registered to EventBus again.
- The background operation which was requested before the user navigated to B is completed, an event X is fired from the background.
- Event X is delivered to A, and the event handler (
onEvent(X event){...}
) navigates the user to activity C immediately.
This is a weird user experience, how can we avoid the event to be delivered to A when it is registered again?
One solution is to cancel the ongoing background operations at onStop()
, so that the event will never be fired; however, cancelling a background operation may not always be possible.
I am using the EventBus of GreenRobot.