Android ASync task ProgressDialog isn't showing until background thread finishes?

Up vote 10 down vote favorite 3 share g+ share fb share tw.

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 { 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(item0); } 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? Android android-asynctask link|improve this question asked Apr 24 '10 at 0:21jackbot8411515 85% accept rate.

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 1: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.

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(); }.

You're just doing the same thing with a slight change. – Scott Biggs Feb 4 at 3:39 this is not the answer. There is no difference with the original code.

– Tim Mar 10 at 18:05.

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.

Upvote for mentioning that a task. Get blocks the ui. I needed to see that somewhere.

Thanks! – WWarrior Mar 24 '11 at 23:50 Having the same problem. What else can block the UI thread besides the get() method?

– Scott Biggs Feb 4 at 3:46.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions