How to cancel handler.postDelayed?
What if I have handler.postDelayed
thread already under execution and I need to cancel it?
Solution 1:
I do this to cancel postDelays, per the Android: removeCallbacks removes any pending posts of Runnable r that are in the message queue.
handler.removeCallbacks(runnableRunner);
or use to remove all messages and callbacks
handler.removeCallbacksAndMessages(null);
Solution 2:
If you don't want to keep a reference of the runnable, you could simply call:
handler.removeCallbacksAndMessages(null);
The official documentation says:
... If token is null, all callbacks and messages will be removed.