Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: What's the difference between Activity.runOnUiThread and View.post?

Tags:

android

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

like image 291
Alexander Kulyakhtin Avatar asked May 11 '12 20:05

Alexander Kulyakhtin


People also ask

What does View Post do Android?

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.

What is looper getMainLooper ()?

static Looper. getMainLooper() Returns the application's main looper, which lives in the main thread of the application. MessageQueue. getQueue()

What is Handler post?

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 .


2 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).

like image 81
MByD Avatar answered Oct 21 '22 14:10

MByD


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.

like image 38
pareshgoel Avatar answered Oct 21 '22 15:10

pareshgoel