Difference between Handler.post(Runnable r) and Activity.runOnUiThread(Runnable r)

VinceFR picture VinceFR · Sep 17, 2011 · Viewed 22.5k times · Source

Is there a difference between

new Handler.post(Runnable r);

and

activity.runOnUiThread(Runnable r)

Answer

Lalit Poptani picture Lalit Poptani · Sep 17, 2011

From the official Handler docs

Handler

There are two main uses for a Handler:

(1) To schedule messages and runnables to be executed as some point in the future.

(2) To enqueue an action to be performed on a different thread than your own.

In short, Handler is used to manage different Runnables.

runOnUiThread

It is used to execute the non-UI operation on the UI Thread, example if you want to update the screen from AsyncTask's doInBackground() you have to write the part of code that update's the UI inside the runOnUiThread(). But again that will block the UI.