How to Cancel All Timers in Android Application If They Exist?

How to cancel all timers in Android application (started by new Timer()) if they exist now running in App? If needed, there is the Activity, that calls them. I have timer.cancel() on Activity destroy.

It's on that case that user will press Home button, then go to Apps, call App again and can launch this Activity with other parameter, so there may be 2 concurrent timers.

Something like "FOREACH TIMER IN TIMERS TIMER.CANCEL()"

1 Answer

You can do something like that:

 List<Timer> timers = new ArrayList<Timer>();
 //every time you starting new timer
 timers.add(new Timer());

 //in onDestroy (or onPause, if you need)
 for (Timer timer : timers) {
 ....
 }
 super.onDestroy(); //super.onPause();      
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest