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?
I have been developing the Android application and I use Timer and TimerTask objects
Share
You can’t reuse
TimerTasks.The reason why you get
IllegalStateExceptionis (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 freshTimerTaskeach time you want to schedule it.