How To Cancel The Execution Of Postdelayed()
I'm making an app where some sort of widgets are created on the fly and can be re-arranged with drag and drop. I'm trying to implement 2 things: Long press to initiate drag and dr
Solution 1:
You have to keep references to your Runnables
and Handler
and use Handler#removeCallbacks()
.
So basically:
Handlerh=newHandler();
Runnabler=newRunnable() { /* does something */
h.postDelayed(r, 1000);
// When you want to cancel.
h.removeCallbacks(r);
Solution 2:
Declare your Runnable
outside of onTouch
event, like seperate method in your Class. Then when you want to actually execute runnable, call your method.
Quick'n'Dirty Example:
onTouch(..) {
if (eventDown) {
// start Runnable
}
if (eventUp) {
// Do not call runnable
}
}
privatevoidexecuteMyRunnable(/* Any arguments you need in Runnable */) {
// Your runnable code here
}
Solution 3:
You can check for long press by taking the time difference between system time at action.down and system time at action.up. If the difference is greater than say 5000 milliseconds, then do the runnable code else the dialog code.
Solution 4:
Simply run handler.removeCallbacksAndMessages(null);
Post a Comment for "How To Cancel The Execution Of Postdelayed()"