I use runOnUIThread
method to pass Runnable
tasks to the main thread queue, but I need to clear all queue tasks, that I sent before, before sending a new one. How to do it?
use an Handler
to post. It has the same effect of runOnUiThread. On your handler instance you can call removeCallbacks(null)
, which will remove every element in the Handler queue, or removeCallbacks(yourannableinstance)
which remove every element of yourannableinstance
kind
You can use the removeCallbacks(Runnable r)
method. If they are anonymous then you can use removeCallbacksAndMessages(null);
. If this doesn't fix the problem please give me more details
The UI thread is also a Looper
thread, and it only have one Message Queue
.
So if you create a handler in UI thread, and then call handler.post(runnable)
, the runnable task will store in the message queue.
If you call runOnUIThread()
, the run task will also store in the same message queue.
5289 public final void runOnUiThread(Runnable action) {
5290 if (Thread.currentThread() != mUiThread) {
5291 mHandler.post(action); // runOnUiThread also calls handler.post()
5292 } else {
5293 action.run();
5294 }
5295 }
And mHandler.removeCallbacksAndMessages(null)
would help you to remove all callbacks and messages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With