Clear UI Thread Queue

Copied from How to clear ui thread queue?

Q: How to clear UI Thread queue?

A: 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 queue. If you call runOnUIThread(), the run task will also store in the SAME queue, refer to the method source code:

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) can remove all callbacks and messages in the queue.