Вопрос

I am displaying a cancelable progress dialog while an asynctask is running. When I press the back button, I want both the dialog to disappear, but also to cancel the running async task. The problem is: The dialog-instance is not known to the asynctask, so I cant't cancel the dialog after the asynctask has been cancelled.

Currently it is only cancelled after I pushed the back button twice.

Это было полезно?

Решение

Dialog boxes have an onDismissed callback, use that to kill of the async task.

Другие советы

You can use ProgressDialog callback on cancel:

this.dialog = new ProgressDialog(context);
this.dialog.setMessage("Loading...");
this.dialog.setCancelable(true);
this.dialog.setOnCancelListener(new DialogInterface.OnCancelListener()
{
    @Override
    public void onCancel(DialogInterface dialog)
    {
        // cancel AsyncTask
        cancel(false);
    }
});
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top