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:

  1. user launches a.
  2. user starts background operation on a.
  3. user navigates b tapping button on without waiting background operation completed. unregistered eventbus.
  4. user navigates a. registered eventbus again.
  5. the background operation requested before user navigated b completed, event x fired background.
  6. 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

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -