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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like