Difference between Handler.post(Runnable r) and Activity.runOnUiThread(Runnable r) [duplicate]
Asked Answered
C

3

35

Is there a difference between

new Handler.post(Runnable r);

and

activity.runOnUiThread(Runnable r)
Chaney answered 17/9, 2011 at 6:16 Comment(0)
E
7

runOnUiThread is basically suited to show a progress dialog or do some UI manipulations before an AsyncTask call. If you want to update the UI in the middle of a thread execution, then the best approach is to create a Handler which will update your UI, and let the thread continue running, for example, updating a text view periodically after a few sec, say timer functionality.

Erickson answered 17/9, 2011 at 7:15 Comment(2)
This answer does not explain the real difference between them yet.Leid
The problem with activity.runOnUiThread(Runnable r) within a thread is that you have to pass a reference of the activity to the thread... which is not going to be a good design as the activity can be destroyed while the thread can continue to run.Quietude
H
19

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.

Hinayana answered 17/9, 2011 at 6:21 Comment(3)
@Lalit - great answer. Could you please tell me whether a runOnUiThread would potentially block the UI (as in, freeze the phone)?Forerun
This doesn't seem correct, if the task that's executed within runOnUiThread takes long enough then it will block the UI thread.Selfseeking
@Aleross You should not run heavy tasks on runOnUiThread, only for updating the ui "quickly".Conidium
E
7

runOnUiThread is basically suited to show a progress dialog or do some UI manipulations before an AsyncTask call. If you want to update the UI in the middle of a thread execution, then the best approach is to create a Handler which will update your UI, and let the thread continue running, for example, updating a text view periodically after a few sec, say timer functionality.

Erickson answered 17/9, 2011 at 7:15 Comment(2)
This answer does not explain the real difference between them yet.Leid
The problem with activity.runOnUiThread(Runnable r) within a thread is that you have to pass a reference of the activity to the thread... which is not going to be a good design as the activity can be destroyed while the thread can continue to run.Quietude
P
1

A Handler is attached to the thread it was created on.

handler.post(Runnable) can be used to run code on the thread Handler is attached to.

Activity.runOnUIThread(Runnable) always run the given runnable on the activity's UIThread. Internnaly it does so through a handler Activity creates when constructed like so:

final Handler mHandler = new Handler();

Hence runonUiThrad code looks like this:

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

As you can see if the current thread is not the UI thread, it posts the given runnable on its member handler which we referred to earlier. If the caller is already on the ui thread, it just calls the runnable.

Rad the code here.

Premonition answered 18/12, 2017 at 10:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.