views:

148

answers:

1

I am writing a count down timer, using .postdelayed(), that speaks each second as the timer goes down to zero. I have trouble understanding the consequences of the user hitting the back or home button. The countdown continues when hitting either button. When home is pressed and you reclick the app, it goes back to the same count(this is what i always want) and continues to count down. When you hit the back key and then reclick the app, you get a new clock that has not started but I still here the sounds from the original timer. How can I code an app so it only can have one instance and chicking the app icon will always go to the active version if it is running?

A: 

Take a look of this Question... it's basically what you need. Now... if you wonder about this behaivor it's just that when you press the Home Button your application is not destroyed but paused. But, when you press the Back Button, the current activity is pushed from the stack, and that means your application is destroyed (so you better save the state of your application).

Cristian
Thanks Cristian, I understand that I will need to save the state of the app, and that that if the app is destroyed I can reopen the app and reset the state. To though the app wasn't destroyed good enough. I can can still hear the short .mp3 files firing and that means the runnable clock handler is still going and setting new postdelayed()s. If I hit the app icon and restore the state i still feel there are 2 instances of the timer. Can I catch the destroy in code?
Tim Wayne
Of course... you can implement the `onDestroy` method on your activity, save the state and kill the thread that is speaking.
Cristian
excellent! Thank you.
Tim Wayne