Doing UI task in doinbackground() in Android
Solution 1:
Hope this will solve your problem
onPreExecute() {
// some code #1
}
doInBackground() {
runOnUiThread(new Runnable() {
public void run() {
// some code #3 (Write your code here to run in UI thread)
}
});
}
onPostExecute() {
// some code #3
}
Solution 2:
Other than updating UI from onPostExecute()
, there are 2 ways to update UI:
- From
doInBackground()
by implementingrunOnUiThread()
- From
onProgressUpdate()
FYI,
onProgressUpdate()
- Whenever you want to update anything from doInBackground()
, You just need to use publishProgress(Progress...)
to publish one or more units of progress, it will ping onProgressUpdate()
to update on UI thread.
Solution 3:
you could utilize onProgressUpdate
rather
Docs