Android: Cancel Async Task Android: Cancel Async Task android android

Android: Cancel Async Task


From SDK:

Cancelling a task

A task can be cancelled at any time by invoking cancel(boolean). Invoking this method will cause subsequent calls to isCancelled() to return true.

After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns.

To ensure that a task is cancelled as quickly as possible, you should always check the return value of isCancelled() periodically from doInBackground(Object[]), if possible (inside a loop for instance.)

So your code is right for dialog listener:

uploadingDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {    public void onCancel(DialogInterface dialog) {        myTask.cancel(true);        //finish();    }});

Now, as I have mentioned earlier from SDK, you have to check whether the task is cancelled or not, for that you have to check isCancelled() inside the onPreExecute() method.

For example:

if (isCancelled())     break;else{   // do your work here}


FOUND THE SOLUTION:I added an action listener before uploadingDialog.show() like this:

    uploadingDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){          public void onCancel(DialogInterface dialog) {              myTask.cancel(true);              //finish();          }    });

That way when I press the back button, the above OnCancelListener cancels both dialog and task. Also you can add finish() if you want to finish the whole activity on back pressed. Remember to declare your async task as a variable like this:

    MyAsyncTask myTask=null;

and execute your async task like this:

    myTask = new MyAsyncTask();    myTask.execute();


I spent a while figuring this out, all I wanted was a simple example of how to do it, so I thought I'd post how I did it. This is some code that updates a library and has a progress dialog showing how many books have been updated and cancels when a user dismisses the dialog:

private class UpdateLibrary extends AsyncTask<Void, Integer, Boolean>{    private ProgressDialog dialog = new ProgressDialog(Library.this);    private int total = Library.instance.appState.getAvailableText().length;    private int count = 0;    //Used as handler to cancel task if back button is pressed    private AsyncTask<Void, Integer, Boolean> updateTask = null;    @Override    protected void onPreExecute(){        updateTask = this;        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);        dialog.setOnDismissListener(new OnDismissListener() {                           @Override            public void onDismiss(DialogInterface dialog) {                updateTask.cancel(true);            }        });        dialog.setMessage("Updating Library...");        dialog.setMax(total);        dialog.show();    }    @Override    protected Boolean doInBackground(Void... arg0) {            for (int i = 0; i < appState.getAvailableText().length;i++){                if(isCancelled()){                    break;                }                //Do your updating stuff here            }        }    @Override    protected void onProgressUpdate(Integer... progress){        count += progress[0];        dialog.setProgress(count);    }    @Override    protected void onPostExecute(Boolean finished){        dialog.dismiss();        if (finished)            DialogHelper.showMessage(Str.TEXT_UPDATELIBRARY, Str.TEXT_UPDATECOMPLETED, Library.instance);        else             DialogHelper.showMessage(Str.TEXT_UPDATELIBRARY,Str.TEXT_NOUPDATE , Library.instance);    }}