I am new to Android . As per the Android Developers Doc making an activity launchmode singleTop it will keep that activity intact . But its not working for me .I have an Activity where i have a countdown timer , what i want is when i leave that Activity on back press and return to that Activity that countdown timer should still be running . How to do it ? Please Help
-
2Create a service that should keep updating the UI. – Sahil Mahajan Mj Jan 04 '13 at 09:53
-
Creating a Service will always keep that timer running , instead i want it to keep running only when application is alive – user1924779 Jan 04 '13 at 09:56
-
use `Service` class , refer the android doc , you can stop the service when the countdowntimer value becomes 0 – Houcine Jan 04 '13 at 09:56
-
1you can stop the service, when your application ends. – Sahil Mahajan Mj Jan 04 '13 at 09:57
-
The problem is unlike iOS there is no really 'easy' way to see if the application is open/running without a few hacks checking states. – Chris.Jenkins Jan 04 '13 at 09:57
-
yaa its easy in ios but what about android ? without using background service is der any other way to do it ? – user1924779 Jan 04 '13 at 09:58
-
@SahilMahajanMj The Application class does not call terminate when your app ends it is very difficult to tell when an app has actually stopped from it's self. – Chris.Jenkins Jan 04 '13 at 09:58
-
@Chris.Jenkins : Cant we check from the service that whether the app is running or not. – Sahil Mahajan Mj Jan 04 '13 at 10:00
-
As you know if your Service is started on your Application UI thread then your Service stop when Application is closed. – user370305 Jan 04 '13 at 10:03
-
@SahilMahajanMj If the application class is not running then the service isn't running. Standard rule, If you have a context then you have the application class. I would say tie this to your application class, but that is bad. Remembering that your application can be killed at any point, you don't get any call backs prior to it being killed. (it is a bit of a pain!). – Chris.Jenkins Jan 04 '13 at 10:06
-
Also you can initiate service with two approach as for Application scope and as an Independent from Application. – user370305 Jan 04 '13 at 10:07
-
What do you mean by "the application running"? When should the countdown timer be stopped? Under what conditions? – David Wasser Jan 04 '13 at 10:08
-
@Chris.Jenkins I think user370305 made a good point in his first comment. – Sahil Mahajan Mj Jan 04 '13 at 10:10
-
@SahilMahajanMj, user370305 is correct. As long as the OP is aware that the device can kill this at anytime. Luckily `onDestroy` should get called. – Chris.Jenkins Jan 04 '13 at 10:14
-
@DavidWasser i simply want that once the timer is started it should continue till the app is running means if the timer is showing 56 when i am pressing back button and spend 5 seconds somewhere else and again come back to that activity it should diplay 51 – user1924779 Jan 04 '13 at 11:05
-
You are confusing BACK with HOME. In Android BACK means that you want to leave the current activity (ie: close it) and that you don't want to come back to it. HOME means that you want to park the current activity for a moment, switch to another activity and then probably return to your activity sometime later. If the user presses BACK he wants to close your activity. – David Wasser Jan 04 '13 at 12:00
-
@DavidWasser- ya thank you for telling me this . I want user can go to any other activity and if he returns to the same activity that timer should still be running. – user1924779 Jan 04 '13 at 13:47
2 Answers
I believe you misunderstood a bit.
Launching an activity in singleTop
does not mean that the activity is "intact", it means that if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent - a new instance won't be created. (This is opposite to launching activities in standard
mode, which every time there's a new intent a new instance of the class is created to respond to that intent.)
As others suggested, you could bind to a Service and update the countdown time from there.

- 15,963
- 5
- 60
- 96
"Creating a Service will always keep that timer running , instead i want it to keep running only when application is alive", that means you want to continue the timer from where the user left.
Store the timer value in SharedPreferences onStop()
and retrieve the same onRestart()
and then finally continue updating

- 3,338
- 2
- 21
- 32