tags:

views:

16

answers:

1

I am working on an android application, where there are 4 activities. Activity1 is main activity and there are different buttons on that activity which can open other activities. Other 3 activities can also open each other. There is a thread running in Activity1 which returns some counter. I need to show that counter on all activities.

At an specific time, the thread doesn't know which activity is at top. What is the right way to control this scenario, such that thread output should update on all activities, no matter which one is at top?

A: 

I would have that counter on a Service and I would send updates through broadcast intent.

Each Activity will register a listener to that intent on the onStart method and unregister it on onPause. This way the update will reach only the Activity that is on screen.

Macarse
OK. What if user starts Activity2 from Activity1 and then Actvity3 from Activity2. Now Activity3 is at top and register to receive broadcast intent. On receiving intent, it displays the updated counter. If user, close the Activity3 by using Back button, the Activity2 comes on top, but it displays the old counter. I want it to show the counter that Activity3 received. In short, at any instant of time, I want all activities to show updated counter.
You can force an update `onStart()` sending an intent to the service.
Macarse