15

I've got an Android activity which grabs an RSS feed from a URL, and uses the SAX parser to stick each item from the XML into an array. This all works fine but, as expected, takes a bit of time, so I want to use AsyncActivity to do it in the background. My code is as follows:

class AddTask extends AsyncTask<Void, Item, Void> {

    protected void onPreExecute() {
        pDialog = ProgressDialog.show(MyActivity.this,"Please wait...", "Retrieving data ...", true);
    }

    protected Void doInBackground(Void... unused) {
        items = parser.getItems();

        for (Item it : items) {
            publishProgress(it);
        }
        return(null);
    }

    protected void onProgressUpdate(Item... item) {
        adapter.add(item[0]);
    }

    protected void onPostExecute(Void unused) {
        pDialog.dismiss();
    }
  }

Which I call in onCreate() with

new AddTask().execute();

The line items = parser.getItems() works fine - items being the arraylist containing each item from the XML. The problem I'm facing is that on starting the activity, the ProgressDialog which i create in onPreExecute() isn't displayed until after the doInBackground() method has finished. i.e. I get a black screen, a long pause, then a completely populated list with the items in. Why is this happening? Why isn't the UI drawing, the ProgressDialog showing, the parser getting the items and incrementally adding them to the list, then the ProgressDialog dismissing?

jackbot
  • 2,931
  • 3
  • 27
  • 35
  • On the "the parser getting the items and incrementally adding them to the list" part, the slow step will be in the parsing, and you aren't adding anything to the list at that point. Hence, all of your items will get slammed into the list fairly quickly, so do not expect much "incremental" effect here. – CommonsWare Apr 24 '10 at 01:08
  • That's no problem, I don't really mind how "incremental" the insertions are, I just want the ProgressDialog to show up while the background work is being done and disappear once it's finished. – jackbot Apr 24 '10 at 11:27

4 Answers4

13

I suspect something is blocking your UI thread after you execute the task. For example, I have seen folks do things like this:

MyTask myTask = new MyTask();
TaskParams params = new TaskParams();
myTask.execute(params);
myTask.get(5000, TimeUnit.MILLISECONDS);

The get invocation here is going to block the UI thread (which presumably is spinning off the task here...) which will prevent any UI related stuff in your task's onPreExecute() method until the task actually completes. Whoops! Hope this helps.

jengelsma
  • 8,192
  • 4
  • 21
  • 21
11

This works for me

@Override
protected void onPreExecute() {
        dialog = new ProgressDialog(viewContacts.this);
        dialog.setMessage(getString(R.string.please_wait_while_loading));
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        dialog.show();
    }
Pentium10
  • 204,586
  • 122
  • 423
  • 502
4

It is because you used AsyncTask.get() that blocks the UI thread "Waits if necessary for the computation to complete, and then retrieves its result.".

The right way to do it is to pass Activity instance to your AsyncTask by constructor, and finish whatever you want to do in AsyncTask.onPostExecution().

Szymon
  • 42,577
  • 16
  • 96
  • 114
1

If you subclass the AsyncTask in your actual Activity, you can use the onPostExecute method to assign the result of the background work to a member of your calling class.

The result is passed as a parameter in this method, if specified as the third generic type.

This way, your UI Thread won't be blocked as mentioned above. You have to take care of any subsequent usage of the result outside the subclass though, as the background thread could still be running and your member wouldn't have the new value.

GeHa
  • 11
  • 3