Take the 2-minute tour ×

I have an Android app. I have a main activity, that has a button. When the button is clicked, another activity comes to the foreground. The thing is, I want to run a background thread that polls updates from the server. However, I want it to run only when the app is in foreground (either the main activity or the second one), and to stop polling when the user clicks the Home button or clicks the Back button till it's going back from the main activity.

But how do I know if the app is still in the foreground? I can catch the onPause of the main activity, but it's called also when I'm launching the second activity.

So how do I know when the app is in background?


share|improve this question

3 Answers 3

You should make a Service for the work you are doing in the background.

For stopping it when you click the Home or Back button, just make a listener for them and stop the Service when either one is pressed.

share|improve this answer
You can't listen for HOME developer.android.com/reference/android/view/… –  dten Aug 5 '11 at 18:55

Seems easiest to me that each activity polls. Is it super important that it can poll when it is between the two activities? Otherwise you will have problems about knowing who is in front or not.

share|improve this answer

You can have a singleton with reference counting. You main activity should add the first reference on it's onResume and from now, upon calling for every new activity (startActivity for example) you should add a reference. Each activity should decrease the reference counting on its onPause.

Another option is to use services: Services

share|improve this answer
Let's say I call +1 in onResume and -1 on onPause (is that what you meant?) - since the first onPause is called before the second onResume, won't the ref count be 0? I thought there's a known design pattern, maybe combining Application and Activity? Besides, how would a service help? I would still like to know whether the app is in foreground or not... –  user449265 Aug 5 '11 at 20:13
This solution is close, but doesn't work: if activity A starts activity B, then clicking back on B and clicking home on B looks the same: it calls onPause in B without first calling onResume on A. –  user449265 Aug 7 '11 at 10:43
One other problem: activity A starts activity B, which starts C. A does +1 in the onResume, then B before the startActivity, (that's +2) then A does -1 on onPause. Same for C, now we're in C in the foreground with +1 in the refCount. Then the user clicks back, and we're back in B with 0 in the refCount –  user449265 Aug 8 '11 at 6:51
Yep, you're right. I don't know if it is feasible for you're application type but how about working with only 1 activity and only swapping the views? –  Lior Ohana Aug 8 '11 at 8:31
Don't like one activity solution, cause it's too much to handle, and the activities manage it right. However, when I changed your solution from onResume/onPause to onStart/onStop, everything was ok, except for one thing: when the device is locked (either timeout, or button) only the onPause callback is called... any suggestion? Do you think it would matter if I used services? –  user449265 Aug 14 '11 at 12:57

Your Answer

