Skip to content Skip to sidebar Skip to footer

How To Dismiss Dialog While Doinbackground Is Running In Asynctask?

I am trying to cancel a dialog from the mainthread while the 'doInBackGround' method of AsyncTask is running. While I am downloading a photo, a progress dialog pops up and when it

Solution 1:

Use a cancellable progress dialog, pass in a cancel listener to the progress dialog and cancel the task within that method, eg

protectedvoidonPreExecute() {
    progressDialog = ProgressDialog.show(activity, "Searching files", "Scanning...", true, true,
            newDialogInterface.OnCancelListener() {
                @OverridepublicvoidonCancel(DialogInterface dialog) {
                    // When dialog in cancelled, need to explicitly cancel task otherwise it keeps on runningcancel(true);
                }
            }
    );

    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
}

Solution 2:

You can intercept the onBackKeyPressed event, and cancel the task using cancel method.

See that link: Ideal way to cancel an executing AsyncTask

Solution 3:

You can try to use a ProgressDialog which is cancelable. This is the signature of the method:-

publicstatic ProgressDialog show(Context context, CharSequence title, CharSequence message, boolean indeterminate, boolean cancelable)

Post a Comment for "How To Dismiss Dialog While Doinbackground Is Running In Asynctask?"