120

Can anyone tell me if there's any difference between using runOnUiThread() versus Looper.getMainLooper().post() to execute a task on the UI thread in Android??

The only thing I can determine is that since runOnUiThread is a non-static Activity method, Looper.getMainLooper().post() is more convenient when you need to code something in a class that can't see the Activity (such as an interface).

I'm not looking for a discussion on WHETHER something should be executed on the UI thread, I get that some things can't and a great many things shouldn't, however, some things (like starting up an AsyncTask) MUST be executed from the UI thread.

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
Rich
  • 4,157
  • 5
  • 33
  • 45
  • 7
    There is no difference except that `runOnUiThread` will check if it is already the UI thread and execute your task directly instead of posting it as a `Message` – zapl Dec 20 '12 at 14:52
  • 1
    Thanks. Would you please convert that to an answer, so I can accept it?? – Rich Dec 20 '12 at 14:54
  • Also, I've already written some code to check if something is being executed on the UI thread, so that would be very simple to include manually. – Rich Dec 20 '12 at 15:01

1 Answers1

221

The following behaves the same when called from background threads:

  • using Looper.getMainLooper()

    Runnable task = getTask();
    new Handler(Looper.getMainLooper()).post(task);
    
  • using Activity#runOnUiThread()

    Runnable task = getTask();
    runOnUiThread(task);
    

The only difference is when you do that from the UI thread since

public final void runOnUiThread(Runnable action) {
    if (Thread.currentThread() != mUiThread) {
        mHandler.post(action);
    } else {
        action.run();
    }
}

will check if the current Thread is already the UI thread and then execute it directly. Posting it as a message will delay the execution until you return from the current UI-thread method.

There is also a third way to execute a Runnable on the UI thread which would be View#post(Runnable) - this one will always post the message even when called from the UI thread. That is useful since that will ensure that the View has been properly constructed and has a layout before the code is executed.

Marian Paździoch
  • 8,813
  • 10
  • 58
  • 103
zapl
  • 63,179
  • 10
  • 123
  • 154