0

I have progressDialog, which is used for my AsyncTask, while downloading file. In AsyncTask I have implemented theese methods:

@Override
protected void onCancelled() {
  handleOnCancelled(this.result);
  super.onCancelled();
}


@Override
protected void onCancelled(String result) {
    super.onCancelled(result);

}

In my activty, I am declaring mProgressDialog and giving it onCancelListener:

mProgressDialog.setOnCancelListener(new OnCancelListener() {

    @Override
    public void onCancel(DialogInterface dialog) {
       xml.cancel(true);


    }
});

When I press back key, mProgressDialog is closed, onCancel (method above) is called, but Async Task still runs on the background. How to solve it?

Thanks

Waypoint
  • 17,283
  • 39
  • 116
  • 170
  • possible duplicate: http://stackoverflow.com/questions/6039158/android-cancel-async-task – royB Mar 11 '15 at 13:58

3 Answers3

3

You need to repeatedly check the boolean isCanceled() within your doInBackground() method. If this method returns true, you should immediately exit any loop or background work being done by the task.

Refer to the docs.

0

Try This

if(myAsyncTask.getStatus().equals(AsyncTask.Status.RUNNING))
{    
      myAsyncTask.cancel(true);
}
0

Check for isCanceled() in the async task while you are downloading.

danny117
  • 5,581
  • 1
  • 26
  • 35