How to use runOnUiThread ()

5

I am studying the use of Threads, aSynkTasks and Handlers and I came across this method, runOnUiThread ()

  • How does this method become a repetitive process to the point of replacing the handler? does this method work?

  • I would like to understand how this method works and what need to implement Runnable () within this method as in this example:


runOnUiThread(new Runnable())
public void run(){
// alguma coisa}
    
asked by anonymous 06.04.2017 / 14:46

1 answer

5

Method definition:

  

"Performs the action specified in the UI Thread. If the current Thread is the   Thread of the UI, then the action will be executed immediately. If the Thread   current is not from the UI, the action is thrown in the event queue. "

So let's go:

  • How does this method become a repetitive process to the point of replacing the handler? How does this method work?

I believe it does not replace Handler because they have different features!

The runOnUiThread , sends a Runnable to run on the UI Thread, while Handler , executes a Runnable at a future time (predefined) or executes this Runnable on a Thread different (not just UI as runOnUiThread ).

  • I would like to understand the operation of this method and the need to implement Runnable () within this method

I believe the above definition explains its operation!

Now let's set the Runnable :

  

The Runnable interface should be implemented by any class whose   instances are meant to be run by a Thread.

We use Runnable to encapsulate and send the code we want to transfer to run on another Thread .

runOnUiThread method documentation

Handler Class Documentation

Runnable Interface Documentation

    
06.04.2017 / 15:43