I have a question regarding the threads in Android programming language; what is the proper method to use in a thread to call the UI thread? is it runOnUIthread method or AsyncTask method ? with arguments please? Thanks for your advices.
-
If you're already in a separate `Thread`, you want to use `runOnUiThread()`. `AsyncTask` is a separate way of handling threads in general. – Geobits Sep 27 '13 at 13:46
2 Answers
AsyncTask is a facility providing a easy use of a Thread and the communication with the UI-Thread.
The method runOnUiThread is no Thread. It is used to communicate with the UI-Thread inside a background Thread. E.g. You have some calculation in a Thread and you want to update a View. To accomplish this you have to run the part, which is modifying the UI, on the UI-Thread. Therefor you can use this method.

- 1
- 1

- 12,843
- 7
- 59
- 79
It depends on what you are trying to accomplish. If it is a small one-off task like downloading a file over http then AsyncTask is probably going to be simpler to implement.
However, if you have a background thread running for a long time and need to publish updates to the view hierarchy before the task is finished, then runOnUiThread will be a good choice.
The key difference is that AsyncTask requires you to wait until your task is finished to publish updates.
EDIT: As Geobits points out, you do not have to wait until the task is complete to publish updates as AsyncTask has a publishProgress method for exactly that.

- 1,496
- 10
- 14
-
"The key difference is..." AsyncTask has a `onProgressUpdate()` method that runs on the UI thread and can be called from anywhere in `doInBackground()` via `publishProgress()`. It's misleading to say you must wait until the task is finished to publish updates. – Geobits Sep 27 '13 at 13:44