2

I am using AsyncTask in my Android application.

Here is my task:

public class MyTask extends AsyncTask<String, String, Boolean> {
    private ProgressDialog progressDialog;
    private boolean isCancelled = false;
    public MyTask(ProgressDialog progressDialog) {
        this.progressDialog = progressDialog;
    }

    @Override
    protected Boolean doInBackground(String... params) {
        try {
            if (!isCancelled()) {
                isCancelled = false;
            } else
                isCancelled = true;
        } catch (Exception e) {

            isCancelled = true;
        }
        return isCancelled;
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
        isCancelled = true;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        progressDialog.dismiss();
        if (!isCancelled) {
            // start an activity
        }
    }
}

I want to cancel this task when pressing device's back button and also cancel the ProgressDialog, but this task executes quickly. When the back button is pressed, the ProgressDialog is cancelled, but the task completes.

This AsyncTask is invoked from an activity like this:

ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading");
progressDialog.setOnCancelListener(new OnCancelListener() {
    @Override
    public void onCancel(DialogInterface dialog) {
        if (myTask!= null
                && myTask.getStatus() != AsyncTask.Status.FINISHED
                && !myTask.isCancelled()) {
        myTask.cancel(true);
        }                       
    }
});

progressDialog.show();
myTask = new MyTask(progressDialog);
myTask.execute();

When logging, I found that the dialog is dismissed (invokes onDismissListener) after executing the condition of onPostExecute(). How can I cancel this task properly?

My intention is cancel the task with back button press whether the task completes or not. Is it possible to cancel an AsyncTask from its onPostExecute()?

Ry-
  • 218,210
  • 55
  • 464
  • 476
Asha S
  • 51
  • 1
  • 7
  • a good example on correct way to cancel and asynctask in android http://www.quicktips.in/correct-way-to-cancel-an-asynctask-in-android/ – Deepak Swami Jun 29 '16 at 17:08

1 Answers1

1

Actually, your code looks right,

In ProgressDialog's OnCancel()

After invoking myTask.cancel(true); method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]).

Note:

If you call cancel(true), an interrupt will be sent to the background thread, 
which may help interruptible tasks. Otherwise, you should simply make sure to check
isCancelled() regularly in your doInBackground() method. 

You can see examples of this at http://code.google.com/p/shelves.

But I suggest you to not canceling AsyncTask and just maintain your boolean Flag only dismiss the dialog on back pressed and onPostExecute() of AsyncTask decide what to do with result using your boolean flag condition.

user370305
  • 108,599
  • 23
  • 164
  • 151
  • How to check isCancelled() regularly in this code? progressDialog.dismiss() executes after executing if(!isCancelled) condition. – Asha S Sep 04 '12 at 07:01
  • Have you tired to starting ProgressDialog in `onPreExecute()` of AsyncTask then only `onCancel()` of ProgressDialog will execute when user press back key or your ProgressDialog's `dismiss()` will execute. – user370305 Sep 04 '12 at 07:07
  • Also Look at this SO Question http://stackoverflow.com/questions/4748964/android-cancel-asynctask-forcefully – user370305 Sep 04 '12 at 07:09
  • Is it possible to cancel an asyncTask from onPostExecute()? – Asha S Sep 04 '12 at 07:41
  • Then there is no need of Cancelling AsyncTask. Simply skips the code you want to execute onPostExecute(). And AsyncTask will automatically cancelled after onPostExecute() completes.. – user370305 Sep 04 '12 at 07:43