269

I am using handler.postDelayed() to create a waiting period before the next stage of my app takes place. During the wait period I am displaying a dialog with progress bar and cancel button.

My problem is I can't find a way to cancel the postDelayed task before the time elapses.

2

5 Answers 5

495

I do this to post a delayed runnable:

myHandler.postDelayed(myRunnable, SPLASH_DISPLAY_LENGTH); 

And this to remove it: myHandler.removeCallbacks(myRunnable);

2
  • 77
    If you can afford to cancel all callbacks and messages on the handler and don't want to have to keep references to the runnable the third point in the accepted answer to this question is another alternative that seems to work for my case anyway: stackoverflow.com/questions/11299440/… (essentially calling myHandler.removeCallbacksAndMessages(null); )
    – Mick
    Oct 26, 2012 at 11:03
  • removeCallbacksAndMessages can do the trick, but personally I prefer to have the control over the scheduled runnables. Instancing and handling a pair of Runnables won't kill you app performances; otherwise, if you need more than two or three Runnables, you may need something more powerful, imho. Jul 13, 2015 at 13:05
71

In case you do have multiple inner/anonymous runnables passed to same handler, and you want to cancel all at same event use

handler.removeCallbacksAndMessages(null);

As per documentation,

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

1
  • This will remove all runnable callbacks once. Otherwise, call removeCallbacks(callback) to remove specific callback Jun 24, 2018 at 2:35
17

Another way is to handle the Runnable itself:

Runnable r = new Runnable {
    public void run() {
        if (booleanCancelMember != false) {
            //do what you need
        }
    }
}
10
  • 14
    Wouldn't booleanCancelMember have to be final, which means it couldn't be changed and thus useless for this purpose? Jun 26, 2011 at 21:04
  • 9
    @stealthcopter no it does not have to be. Jul 20, 2012 at 12:16
  • 7
    @pablisco It doesn't have to be final even if the Runnable is anonymous. It can be a member in the enclosing class.
    – Martin
    Apr 20, 2013 at 5:36
  • 6
    Not downvoting this, but be careful. With this approach, if multiple Runnables are posted delayed, you may run into nasty race conditions. This doesn't cancel the Runnable, so whether or not that code will be execute depends on the value of booleanCancelMember at the specific moment for each posted Runnable. Which may quickly become unpredictable.
    – Dennis K
    Dec 20, 2013 at 20:45
  • 1
    If the Runnable is NOT annonymous it means I have a reference to it (r in this case) which means I can use myHandler.removeCallbacks(r);. If the Runnable is anonymous then the flag will be a member in the enclosing class, which means I need a reference to that object to change the flag, which means I again will have to have r anyways, which means I can do myHandler.removeCallbacks(r); . And if I am doing myHandler.removeCallbacks(r); then such flag is not necessary at all. Am I missing something?
    – nacho4d
    Jun 28, 2016 at 13:52
1

Here is a class providing a cancel method for a delayed action

public class DelayedAction {

private Handler _handler;
private Runnable _runnable;

/**
 * Constructor
 * @param runnable The runnable
 * @param delay The delay (in milli sec) to wait before running the runnable
 */
public DelayedAction(Runnable runnable, long delay) {
    _handler = new Handler(Looper.getMainLooper());
    _runnable = runnable;
    _handler.postDelayed(_runnable, delay);
}

/**
 * Cancel a runnable
 */
public void cancel() {
    if ( _handler == null || _runnable == null ) {
        return;
    }
    _handler.removeCallbacks(_runnable);
}}
0

It worked for me when I called CancelCallBacks(this) inside the post delayed runnable by handing it via a boolean

Runnable runnable = new Runnable(){
    @Override
    public void run() {
        Log.e("HANDLER", "run: Outside Runnable");
        if (IsRecording) {
            Log.e("HANDLER", "run: Runnable");
            handler.postDelayed(this, 2000);
        }else{
            handler.removeCallbacks(this);
        }
    }
};
1
  • 3
    There is no "CancelCallBacks" in your code. Maybe 'cause it doesn't exist? :) Jun 1, 2017 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.