Is CountDownTimer cancelled automatically on Activity destroy()?
Asked Answered
B

1

8

A question, maybe a little naive...

If I start a 2 minute CountDownTimer in Activity onCreate() and close it with finish() when user clicks a button, does Android cancel it automatically?

Or do I need to explicitly call cancel() in onDestroy()?

Breeches answered 27/8, 2012 at 10:26 Comment(2)
Have you ever tried it? Just put a Log.d(...) in onFinish() and see if it is logged.Orphrey
Just tried. onFinish() seems alive even after an Activity.finish().Trompe
L
4

You will have to explicitly call cancel in Ondestroy. But better practice is to call cancel in Onpause.
Since if you have paused your activity and you update any Ui elements inside OnFinish of the countdown timer it may throw errors.

Lysozyme answered 27/8, 2012 at 10:37 Comment(1)
Actually onDestroy() is much better and a straight-forward approach. The CountDownTimer won't throw any errors even if its onFinish() is called during onPause(). I tried it.Insinuating

© 2022 - 2024 — McMap. All rights reserved.