Doing UI task in doinbackground() in Android

前端 未结 3 1033
孤城傲影
孤城傲影 2020-11-30 11:36

Is there a way to do UI task in the doinbackground() of the AsyncTask. I am well aware it is better to do it in onPostExecute method. But in my case since I am need to use a

相关标签:
3条回答
  • 2020-11-30 12:17

    you could utilize onProgressUpdate rather

    Docs

    0 讨论(0)
  • 2020-11-30 12:28

    Other than updating UI from onPostExecute(), there are 2 ways to update UI:

    1. From doInBackground() by implementing runOnUiThread()
    2. 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.

    0 讨论(0)
  • 2020-11-30 12:30

    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
        }
    
    0 讨论(0)
提交回复
热议问题