Androids Handler.post what happens exactly

from a few days, I tried to figure out what exactly happens if I execute the code in

void function(){ //somePreExecutionCode new Handler().post(new Runnable(){ @Override public void run(){ //someCode } }); } 

It does not seem to block the user interface, so the buttons calling the () function do not get stuck in the pressed position until SomeCode ends. But if somePreExecutionCode launches a progressBar, the progressBar is displayed exactly at the very moment that someCode finished. I know there are AsyncTasks, but is there another possibility?

And what's the difference between

 new Handler().post 

and

 View.post 

?

+7
android runnable handler
source share
3 answers

Putting it simply, there are Looper threads, for example, a UI thread. Such a stream has its own Looper , which starts a message loop for the stream.

Such a thread, as a rule, has a Handler that processes Looper messages - overrides public void handleMessage(Message msg) or runs a Runnable that has been sent to the loop message queue.

When you create a Handler in the context of the user interface thread (as in your code), it is associated with the loopback mechanism of the user interface thread, so your \\someCode works in the user interface thread.

I assume that in your case, the use of new Handler().post(Runnable) and View:post(Runnable) is basically the same, since both add Runnable to the message queue of the UI thread.

But they are not the same.

  • View:post(Runnable) will add Runnable to the message queue of the user interface thread looper;
  • Handler:post(Runnable) will add Runnable to its associated message queue message flow

My explanation is pretty much intuitive, so correct me if I'm wrong.

+3
source share

When the Android application is created, the system creates the main thread of execution. This thread is called the UI thread, and all UI-related operations occur in this thread to avoid synchronization problems.

A Looper instance is created in this thread, which has a MessageQueue . Looper will be in an infinite loop, waiting to read the Message / Runnable posted on the MessageQueue . To add Message7 / Runnable to MessageQueue, Handler .

When you create a Handler instance, it will be associated with the current thread of execution and the Looper instance created on that particular thread.

Therefore, when you send a message through a handler, the message is added to the MessageQueue, which will be read in the FIFO order of Looper and delivered to the target.

new Handler (). post () and View.post are slightly different.

  • When you send messages through View.post, you are guaranteed that the message will be published in the MessageQueue user interface stream, as it internally uses the Handler instance created in the user interface stream.
  • If you create an instance of Handler in a user interface thread and send a message using it in any thread, the message will be sent to the MessageQueue user interface thread.
  • If you create an instance of Handler in a thread other than the UI and post messages using it, they will be published in the Message-Message thread other than the UI.
+8
source share

According to Android View documentation :

Runnable will run in the user interface thread

According to the documentation for the Android handler :

Runnable will be launched in the thread to which this handler is attached.

So, in the case of the handler, you can create it in any thread that you want, this is a kind of anchor that will execute the Runnable that you provided in the thread in which it was created.

In View.post, you will always run Runnable in the uI thread.

0
source share

All Articles