I am using AsyncTask to upload data to UI. i wrote the code to download data from server in a separate method and i am calling that method from doinBackground. It will give error because UI methods can't access from doInBackground.but, i want to access . any alternative process is there to access the UI method from doinBackground.?
4 Answers
any alternative process is there to access the UI method from doinBackground.?
Call publishProgress()
in doInBackground()
. Put your UI-updating logic in onProgressUpdate()
of your AsyncTask
. onProgressUpdate()
will be called on the main application thread (a.k.a., UI thread) after you call publishProgress()
. Here is a sample project demonstrating this.

- 986,068
- 189
- 2,389
- 2,491
-
14CommonsWare name is enough . +1 . – Chirag Mar 15 '12 at 12:10
-
knew the runOnUiThread() way but liked the other approachj of handling this +1. – Deva Mar 15 '12 at 12:11
-
Hi i found great answer in the link .Thank you very much – balu... Mar 15 '12 at 12:27
-
hi can i call runOnUiThread() from doInBackground .? it will work.? or as usual can i call runOnUiThread from oncreate itself.? here i have one doubt. runOnUiThread will block the UI while uploading data.? – balu... Mar 15 '12 at 12:05
-
it makes sense when you call it from doInBackground().. or else it has no meaning.. – ngesh Mar 15 '12 at 12:06
-
Use doInBackground()
just for tasks that :
- Take some time
- Are not UI related
Then you can implement AsyncTask.onPostExecute()
to run code to handle those results on main UI thread from AsyncTask
From JavaDoc for AsyncTask.onPostExecute()
:
"Runs on the UI thread after doInBackground. ... "

- 11,284
- 8
- 90
- 137
As the others have pointed out, you can use runOnUiThread. But, it seems a little odd that you would want to do that in your doInBackground. If you are wanting to indicate progress to the user you would want to handle that in AsyncTask.onProgressUpdate and call publishProgress in your doInBackground.
You can read more about AsyncTask here: http://developer.android.com/reference/android/os/AsyncTask.html
-Dan

- 3,884
- 3
- 27
- 32