Take the 2-minute tour ×

I have a handler from my sub-Activity that was called by the main Activity. This handler is used by sub-classes to postDelay some Runnables, and I can't manage them. Now, in onStop event, I need to remove them before finish the activity (somehow I called finish(), but it still call again and again). Is there anyway to remove all callbacks from a Handler?


share|improve this question
add comment

4 Answers

up vote 106 down vote accepted

In my experience calling this worked great!


In the docs for removeCallbacksAndMessages it says...

"Remove any pending posts of callbacks and sent messages whose obj is token. If token is null, all callbacks and messages will be removed."

share|improve this answer
Thank you very much. This question was old, but your information is very helpful! –  DatVM Apr 14 '12 at 6:49
I could find this out when reading the documentation. But I was too lazy to scroll down to detailed method. Thanks Josh, you saved my day :-) –  user942821 May 22 '12 at 3:12
BOSSSSSSSSSSSSSSSS –  tdevoy Jul 1 '13 at 21:38
add comment

If you don't have the Runnable references, on the first callback, get the obj of the message, and use removeCallbacksAndMessages() to remove all related callbacks.

share|improve this answer
add comment

For any specific Runnable instance, call Handler.removeCallbacks(). Note that it uses the Runnable instance itself to determine which callbacks to unregister, so if you are creating a new instance each time a post is made, you need to make sure you have references to the exact Runnable to cancel. Example:

Handler myHandler = new Handler();
Runnable myRunnable = new Runnable() {
    public void run() {
        //Some interesting task

You can call myHandler.postDelayed(myRunnable, x) to post another callback to the message queue at other places in your code, and remove all pending callbacks with myHandler.removeCallbacks(myRunnable)

Unfortunately, you cannot simply "clear" the entire MessageQueue for a Handler, even if you make a request for the MessageQueue object associated with it because the methods for adding and removing items are package protected (only classes within the android.os package can call them). You may have to create a thin Handler subclass to manage a list of Runnables as they are posted/executed...or look at another paradigm for passing your messages between each Activity

Hope that Helps!

share|improve this answer
Thanks, I know that. But I have a lot of Runnable in many sub-classes, and manage them all is a epic work! Is there anyway to remove them all, in the onStop() event? –  DatVM May 4 '11 at 13:15
Understood, I updated the answer with a bit more information. Short version is you can't call a method to broadly clear a Handler's message queue... –  Devunwired May 4 '11 at 14:45
add comment

You just need to call removeCallbacksAndMessages(null) on the handler object.

share|improve this answer
Welcome to Stack Overflow. If there is already an answer which provides the exact same solution as yours, it's better not to post it. Of course, if you have something else to add, don't be afraid to post it. There's tons of other questions that haven't been answered yet - see Newest Questions for the latest list of questions. –  Qantas 94 Heavy Nov 16 '13 at 5:39
add comment

Your Answer


