views:

36

answers:

1

In my application I use an AsyncTask on start up of my Activity to fetch the ringtone of a particular contact.

It works normally but I have noticed that if the application is stopped twice before the AsyncTask gets to the doInBackground method then when the Activity starts again the AsyncTask fails to run properly, only getting to the onPreExecute() method.

Here is my code:

The AsyncTask itself:


private class SelectRingtoneTask extends AsyncTask<String, Void, Void> {

          // can use UI thread here   
          protected void onPreExecute() {
              Log.d("cda", "Into selectRingToneTask - onPreExecute() - " + selectRingtoneFinished);
          }

          // automatically done on worker thread (separate from UI thread)
          protected Void doInBackground(final String... args) {
              Log.d("cda", "Into selectRingToneTask - !!!!!!!!!!!!!!!");
             getRingTone();
             return null;
          }

          // can use UI thread here
          protected void onPostExecute(final Void unused) {
           selectRingtoneFinished = true;
           Log.d("cda", "Into selectRingToneTask - onPostExecute - " + selectRingtoneFinished);
          }
       }

Where I call the AsyncTask on start up:


if(srtt == null){
srtt = new SelectRingtoneTask();
Log.d("cda", "RingTone - " + srtt.getStatus());
}
srtt.execute();

The problem occur's when I start the activity and close the Activity before the AsyncTask finishes, if this happens once, it seems to be fine but after it happens a second time the AsyncTask will only ever get to the onPreExecute() method and will never complete again until the application is force stopped and restarted.

Has anybody got any idea why this would be happening?

+2  A: 

You need to cancel the AsyncTask when your Activity is destroyed, and in AsyncTask's methods check for isCancelled flag before attempting to work with the fetched results.

I highly recommend reading the source code of Shelves to see how to persist tasks across configuration changes and how to cancel them properly when the activity is destroyed.

Pentium10
Thanks Pentium10, so I should call selectrintongtask.cancel(true) in my Activities onDestroy() method? And in each of the Asynctask ethods implement a check for isCancelled before doing anything in the method? In the shevles surce code is there any particular file tat highlights this best? Thanks again
Donal Rafferty
Just to add I have implemented cancelling of the Task in my onDestroy method and it has caused some strange behaviour, not when I end my activity the first time, the second time I try to start it I only get a blank screen
Donal Rafferty
Check out AddBookActivity.java and UserTask(mAddTask) and SearchTask in that file. http://code.google.com/p/shelves/source/browse/trunk/Shelves/src/org/curiouscreature/android/shelves/activity/AddBookActivity.java
Pentium10
Thanks again, I am confused as to the purpose of UserTask, is it a different implementation of AsyncTask? Why dont they just extend AsyncTask?
Donal Rafferty
I don't know, but don't involve in that. Just look how they work out the activity cycle events for the mAddTask variable.
Pentium10
After implementing it exactly the same way as the AddBookActivity in Shelves I still have the same problem, :( maybe its a bug on 1.6?
Donal Rafferty
Ok, I think you should post another question with your full activity source code, and describe the problem you face. Don't forget to mention you are implementing the cancel.
Pentium10