user1099994
user1099994

Reputation: 43

ProgressDialog within AsyncTask not updated

I am having a problem with ProgressDialog UI being frozen when I start the action in the AsyncTask. My problem is somewhat different than the bunch of other similar question because the my background task consists of two parts: - first part (loadDB()) is related to the database access - second part (buildTree()) is related to building the ListView contents and is started with runOnUiThread call

The progress dialog is correctly updated during the 1st part of the task, but not during the 2dn part. I tried moving the buildTree part in the AsyncTask's onPostExecute but it doesn't help, this part of the code still causes the progress to freeze temporarily until this (sometimes quite lengthy) part of the work is done. I can not recode the buildTree part from scratch because it is based on external code I use.

Any tips on how to resolve this? Is there a method to force updating some dialog on screen?

The code goes here:

public class TreePane extends Activity {

   private ProgressDialog progDialog = null;

   public void onCreate(Bundle savedInstanceState) {
       // first setup UI here
       ...

       //now do the lengthy operation
       new LoaderTask().execute();
   }

   protected class LoaderTask extends AsyncTask<Void, Integer, Void>
   {
        protected void onPreExecute() {
           progDialog = new ProgressDialog(TreePane.this);
           progDialog.setMessage("Loading data...");
           progDialog.show();
        }

        protected void onPostExecute(final Void unused) {
            if (progDialog.isShowing()) {
               progDialog.dismiss();
            }
        }

        protected void onProgressUpdate(Integer... progress) {
             //progDialog.setProgress(progress[0]);
        }

        protected Void doInBackground(final Void... unused) 
        {
            //this part does not block progress, that's OK
            loadDB();

            publishProgress(0);

            //long UI thread operation here, blocks progress!!!!
            runOnUiThread(new Runnable() {
                public void run() {
                   buildTree();
                }
            });

            return null;
        }
   }

   public void buildTree()
   {
      //build list view within for loop
      int nCnt = getCountHere();
      for(int =0; i<nCnt; i++)
      {
         progDialog.setProgress(0);
         //add tree item here
      }
   }
}

Upvotes: 1

Views: 1878

Answers (2)

Noureddine AMRI
Noureddine AMRI

Reputation: 2942

Don't run your whole buildTree() method inside the UI thread.

Instead, run only the changes you want to make to the UI in the UI thread:

protected Void doInBackground(final Void... unused) 
{
    //this part does not block progress, that's OK
    loadDB();
    publishProgress(0);
    buildTree();
    return null;
}

And then:

public void buildTree()
{
    //build list view within for loop
    int nCnt = getCountHere();
    for(int =0; i<nCnt; i++)
    {
        progDialog.setProgress(0);
        runOnUiThread(new Runnable() {
            public void run() {
                // update your UI here and return
            }
        });
        // now you can update progress
        publishProgress(i);
    }
}

Upvotes: 2

Asher
Asher

Reputation: 1867

You should call AsyncTask's publishProgress method and not the progDialog.setProgress(0); as you call. Also the buildTree shouln't run on the UI thread since it will block it. run the logic from the doInBackground method.

note that you don't actually build the ListView, rather you should build it's data model. look here

something like this:

protected Void doInBackground(final Void... unused) 
{
    //this part does not block progress, that's OK
    loadDB();
    publishProgress(0);
    buildTree();
}

public void buildTree()
{
    //build list view within for loop
    int nCnt = getCountHere();
    for(int =0; i<nCnt; i++)
    {
        publishProgress(i); //for exmple...
        //add tree item here
    }
}

Upvotes: 0

Related Questions