简体   繁体   中英

Cancelling AsyncTask in Android

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() ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM