android - How to prevent EventBus from delivering event to wrong Activity -
suppose our app has 3 activities: a, b , c. , registered eventbus @ onstart()
, unregistered @ onstop()
explained here, , has event handler method: onevent(x event){...}
navigates user c upon receiving of event x.
but see below unhappy scenario:
- user launches a.
- user starts background operation on a.
- user navigates b tapping button on without waiting background operation completed. unregistered eventbus.
- user navigates a. registered eventbus again.
- the background operation requested before user navigated b completed, event x fired background.
- event x delivered a, , event handler (
onevent(x event){...}
) navigates user activity c immediately.
this weird user experience, how can avoid event delivered when registered again?
one solution cancel ongoing background operations @ onstop()
, event never fired; however, cancelling background operation may not possible.
i using eventbus of greenrobot.
you can use counter bus
for example have static int
field in activity , increase every time register bus
activity , when initializing background task, pass counter task , send when completed. in event handler, check value current value of counter , if not same , nothing.
Comments
Post a Comment