since several days, I tried to figure out what exactly happens if I execute code in
void function(){
//somePreExecutionCode
new Handler().post(new Runnable(){
@Override
public void run(){
//someCode
}
});
}
It seems like it isn't blocking the UI, so buttons, which calls function() doesn't stuck in the clicked position until someCode has finished. But if somePreExecutionCode starts a progressBar, the progressBar is shown at exactly the same moment, when someCode has finished. I know, there are AsyncTasks for, but is there any other possibility?
And whats the difference between
new Handler().post
and
View.post
?
Putting it simply, there are Looper threads, for example, UI thread. Such thread has its own Looper, which runs a message loop for the thread.
Such thread, typically, has a Handler, which processes its Looper
's messages - overriding public void handleMessage(Message msg)
or executing a Runnable
, which was posted to it's looper's message queue.
When you're creating a Handler
in the context of UI thread (like you did in your code), it gets associated with UI thread's looper, so your \\someCode
runs on UI thread.
I guess, in your use case new Handler().post(Runnable)
and View:post(Runnable)
are mostly the same, as they both add a Runnable
to the UI thread message queue.
But they are not the same.
View:post(Runnable)
will add a Runnable
to the UI thread looper's message queue;Handler:post(Runnable)
will add a Runnable
to its associated thread looper's message queueMy explanation is pretty much intuitive, so correct me anyone if I am wrong.
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