Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why to use Handlers while runOnUiThread does the same?

I have come across both Handlers and runOnUiThread concepts. But to me it still seems to be a doubt as on which facts do they differ exactly.

They both are intended to do UI actions from a background thread. But what are the factors that are to be considered while we choose among the two methods.

For example consider a Runnable Thread which performs a web service in the background and now I want to update the UI.

What would be the best way to update my UI? Should I go for Handler or runOnUiThread?

I still know I could use a AsyncTask and make use of onPostExecute. But I just want to know the difference.

like image 943
Andro Selva Avatar asked Sep 27 '12 09:09

Andro Selva


People also ask

Why do we use handlers?

There are two main uses for a Handler: (1) to schedule messages and runnables to be executed at some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.

What is the difference between looper and handler?

Each Looper has its own MessageQueue. Looper drains the MessageQueue. Handler is created for some particular Looper and is used to post messages to it and handle these messages when they are processed.

What is difference between thread and handler?

A Thread , generally MainThread or UIThread contains a Looper . When MainThread runs, it will loop the Looper and execute Runnable one by one. When Handler is preferred: When you want to update UI objects (like TextView text) from other thread, it is necessary that UI objects could only be updated in UI Thread.

What is looper message and handler?

Looper is a worker that keeps a thread alive, loops through MessageQueue and sends messages to the corresponding handler to process. Finally Thread gets terminated by calling Looper's quit() method.


1 Answers

Activity.runOnUiThread() is a special case of more generic Handlers. With Handler you can create your own event query within your own thread. Using Handlers instantiated with the default constructor doesn't mean "code will run on UI thread" in general. By default, handlers are bound to the Thread from which they were instantiated from.

To create a Handler that is guaranteed to bind to the UI (main) thread, you should create a Handler object bound to Main Looper like this:

Handler mHandler = new Handler(Looper.getMainLooper()); 

Moreover, if you check the implementation of the runOnUiThread() method, it is using Handler to do the things:

  public final void runOnUiThread(Runnable action) {         if (Thread.currentThread() != mUiThread) {             mHandler.post(action);         } else {             action.run();         }     } 

As you can see from code snippet above, Runnable action will be executed immediately if runOnUiThread() is called from the UI thread. Otherwise, it will post it to the Handler, which will be executed at some point later.

like image 63
HitOdessit Avatar answered Oct 21 '22 13:10

HitOdessit