What's the difference between Activity.runOnUiThread
and View.post
, could someone, please, explain?

- 47,782
- 38
- 107
- 158
-
similar questions with useful answers: [Runnable is posted successfully but not run](http://stackoverflow.com/questions/4083787/runnable-is-posted-successfully-but-not-run), [Whats the difference between Activity.runOnUiThread(runnable action) and Handler.post()?](http://stackoverflow.com/questions/1839625/whats-the-difference-between-activity-runonuithreadrunnable-action-and-handler), [Difference between Handler.post(Runnable r) and Activity.runOnUiThread(Runnable r)](http://stackoverflow.com/questions/7452884/difference-between-handler-postrunnable-r-and-activity-runonuithreadrunnable) – Richard Le Mesurier Jun 27 '12 at 14:34
4 Answers
There is no real difference, except that the View.post
is helpful when you don't have a direct access to the activity.
In both cases, if not on UI thread, Handler#post(Runnable)
will be called behind the scenes.
As CommonsWare mentioned in the comment, there is a difference between the two - when called on Ui thread, Activity#runOnUiThread
will call the run
method directly, while View#post
will post the runnable
on the queue (e.g. call the Handler#post
)
The important point IMO is that both have the same goal, and for whoever use it, there should be no difference (and the implementation may change in the future).
-
76One difference: `runOnUiThread()` checks the current thread and executes the `Runnable` immediately if we happen to be on the main application thread. `post()` always puts the `Runnable` on the queue, no matter what thread it is called upon. – CommonsWare May 11 '12 at 21:21
-
1Thank you I can now see the difference based on your explanation and the @CommonsWare comment. – Alexander Kulyakhtin May 12 '12 at 08:40
-
@CommonsWare : You said runOnUiThread() executes the Runnable immediately. Does it mean that what ever is currently on the UI thread is ignored and this is given first priority? – Ashwin Nov 19 '12 at 13:35
-
4@Ashwin: "You said runOnUiThread() executes the Runnable immediately" -- no, I did not. Please re-read the comment. I said "`runOnUiThread()` checks the current thread and executes the `Runnable` immediately **if we happen to be on the main application thread**" (emphasis added). "Does it mean that what ever is currently on the UI thread is ignored and this is given first priority?" -- "what ever is currently on the UI thread" *is* the `runOnUiThread()` call. – CommonsWare Nov 19 '12 at 13:42
-
@CommonsWare : Sorry. Thanks. What if we call runOnuiThread(some_runnable) from another thread. Then it's put on the UI Thread's queue, right? – Ashwin Nov 19 '12 at 13:47
-
-
@CommonsWare : What will happen if we call handler.sendMessage() or handler.postRunnable() on the UI thread itself.(handler is also defined on the UI thread)? It should not be put on the queue since handlerpostRunnable() itself is on the UI thread, right? – Ashwin Nov 19 '12 at 14:08
-
@Ashwin: AFAIK, only `runOnUiThread()` optimizes for being run on the UI thread itself. If you have further questions on this subject, please ask a fresh StackOverflow question, rather than continuing to post comments here. – CommonsWare Nov 19 '12 at 14:11
-
@CommonsWare,just curious, do you get such kinds of knowledge from the android source or from your practice, maybe something else? – pvllnspk Sep 17 '14 at 22:02
-
1
-
2There *is* a difference. Posting to a view that is not attached to a window will not do anything. Albeit not a *huge* difference, this can cause subtle bugs and is quite annoying to navigate if you are not aware the difference exists. – dcow May 24 '16 at 17:30
-
The first line is misleading. The difference can cause different behaviour in a multithreaded scenario. In one scenario View.post is missing calling the runnable altogether. In the other scenario, it's calling runnables out of order. – Karioki May 03 '17 at 06:01
Another difference between Activity.runOnUiThread and view.post() is that the runnable in view.post() is called after the view is attached to a window.

- 981
- 8
- 12
-
How do you mean shown? Becomes visible? Not called on an invisible view at all? – Alexander Kulyakhtin Mar 28 '14 at 16:23
-
-
5This is the most important difference IMHO. A lot of people uses view.post() to execute stuff that needs to be executed AFTER the view is attached. – Sotti Feb 14 '15 at 22:33
-
3This is not true. This has never been true, but at some point the JavaDoc for View.java did wrongly state that "View.post only works from another thread when the View is attached to a window". This was [fixed](https://github.com/android/platform_frameworks_base/commit/edc900528937cd03f0d3a94fdf73d019324a2054) on Oct. 15, 2012, but took a while to penetrate the minds of Android developers. – Alex Cohn Jun 26 '16 at 11:56
-
Either are acceptable for most situations and for the most part they are interchangeable, but they are subtly different. The biggest difference of course is that one is available from an Activity
and the other from a View
. There's a lot of overlap between those, but sometimes in an Activity
you will not have access to a View
, and sometimes in a View
you will not have access to an Activity
.
One of the edge cases I've encountered with View.post
I mentioned in an answer to another SO question on View.post
: View.post
only works from another thread when the View
is attached to a window. This is rarely a problem, but can occasionally cause the Runnable
to never execute, especially if you call View.post
in the onCreate
method of your Activity
. An alternative is to use Handler.post
which is what Activity.runOnUiThread
and View.post
use under the covers anyway.
(edited for accuracy, added "from another thread")
-
1It can fail when unattached in `onCreate()` also? Hm, I'd expect it to post to the `Handler` supplied by `ViewRoot` in that case. – Jens May 11 '12 at 21:04
-
5@Jens Yeah, I've glanced at the source and `View.post` should add the `Runnable` to a queue to be executed later if it's not attached yet. I haven't dug much deeper in the source, but the [docs](http://developer.android.com/reference/android/view/View.html#post%28java.lang.Runnable%29) do say: "This method can be invoked from outside of the UI thread only when this View is attached to a window." So I think if it's on the current thread, then what you said is true, if it's not then it probably just swallows the `Runnable`. I've certainly had that happen in my code. – kabuko May 11 '12 at 21:21
-
@kabuko Thank you your answer shows it from another point. How it is I can not accept more than 1 answer can't see logic behind that shall adress meta forum – Alexander Kulyakhtin May 12 '12 at 08:41
-
3This is not true. This has never been true, but at some point the JavaDoc for View.java did wrongly state that "View.post only works from another thread when the View is attached to a window". This was [fixed](https://github.com/android/platform_frameworks_base/commit/edc900528937cd03f0d3a94fdf73d019324a2054) on Oct. 15, 2012, but took a while to penetrate the minds of Android developers. – Alex Cohn Jun 26 '16 at 11:56
Another difference: post
is per View; runOnUiThread
is per Activity.
This means it will be possible (in the future?) to do view.getQueue
/ activity.getQueue
and get exactly what you want without your own tracking or filtering code.

- 86,231
- 106
- 366
- 634