Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear UI Thread queue

Tags:

android

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?

like image 890
pvllnspk Avatar asked May 30 '13 14:05

pvllnspk


3 Answers

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

like image 81
Blackbelt Avatar answered Nov 16 '22 05:11

Blackbelt


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

like image 33
i_me_mine Avatar answered Nov 16 '22 06:11

i_me_mine


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.

like image 22
li2 Avatar answered Nov 16 '22 06:11

li2