Reputation: 2801
I have been developing the Android application and I use Timer
and TimerTask
objects for executing one task after particular time. But there is one problem - sometimes I need to cancel timertask, but if I do it that Android will cause an IllegalStateException
. How can I cancel the task safely?
Upvotes: 3
Views: 463
Reputation: 420951
You can't reuse TimerTask
s.
The reason why you get IllegalStateException
is (most likely) because you try to schedule a task that has previously been canceled.
To do it properly, i.e. to avoid the IllegalStateException
, you need to create a fresh TimerTask
each time you want to schedule it.
Upvotes: 4