What's the difference between Activity.runOnUiThread
and View.post
, could someone, please, explain?
View. post actually queues the animation on the View's message loop, so once the view gets attached to the window, it executes the animation instead of having it execute manually. Yes, the Runnable (code) is posted to a Handler which is (assumed) to be associated with the UI(main) thread.
static Looper. getMainLooper() Returns the application's main looper, which lives in the main thread of the application. MessageQueue. getQueue()
A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue . Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler it is bound to a Looper .
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With