Dayerman
Dayerman

Reputation: 4003

Stop method inside doInBackground of doAsyncTask

I have this AsyncTask:

private class GetMyFlights extends AsyncTask<String, Void, Integer> {       
    private ProgressDialog dialog;  

    public GetMyFlights(ListActivity activity) {}           

    @Override
    protected Integer doInBackground(String... params) {
        return getData();
    }       

    @Override
    protected void onPostExecute(Integer result) {  
        super.onPostExecute(result);    
        //...
    }
}

When I change to another activity I want to stop it. So I set this:

@Override
protected void onPause() {
    if(mGetMyFlights != null){
        mGetMyFlights.cancel(true);
        Log.d(TAG, "MyFlights onPause, cancel task");
    }
    super.onPause();
}

But the code inside getData is still working when I change my activity. How can I be sure is stop?

Upvotes: 0

Views: 3076

Answers (2)

Felix
Felix

Reputation: 89576

What I usually do is call cancel(true) on the AsyncTask instance and check Thread.interrupted() in doInBackground. You could check isCancelled(), but that doesn't work if the actual work is done in some other class that is independent from your AsyncTask and doesn't know about it. For example (copied directly from my own getData() method, inside my Data class, independent from any activities, async tasks, etc.):

while ((line = reader.readLine()) != null) {
    if (Thread.interrupted()) throw new InterruptedException();
    content.append(line);
}

Just make sure to handle the InterruptedException in your doInBackground(), e.g.:

@Override
protected Integer doInBackground(String... params) {
    try {
        return getData();
    }
    catch (InterruptedException e) {
        Log.d("MyApp", "Girl, Interrupted");
        return -1;
    }
}

Also worth noting that onPostExecute() is not called if the task is cancelled. Instead, onCancelled() is called.

Upvotes: 1

Steve Bergamini
Steve Bergamini

Reputation: 14600

From everything I've read, it seems that the cancel() method is not a reliable way to stop an AsyncTask. An interrupt is sent to the background thread, but this is only effective on interruptable tasks. The general consensus is that, to ensure the AsynTask is stopped, you should continually check isCancelled() within the doInBackground method of your AsyncTask.

Upvotes: 2

Related Questions