0

Using the following code which cancels the AsyncTask when the user clicks on the BACK key of the mobile device.

protected class UploadFiles extends AsyncTask<String, String, String> implements OnDismissListener {

private ProgressDialog mDialog;

protected void onPreExecute() {

super.onPreExecute();
mDialog = new ProgressDialog(UploadFiles.this);
mDialog.setMessage(progressMsg);
mDialog.setCancelable(true);
mDialog.setOnDismissListener(this);
mDialog.show();

}

protected String doInBackground(String... params) {

........
........

}

public void onDismiss(DialogInterface dialog) {

this.cancel(true);
}

protected void onCancelled() {

cancel(true);

}

The above implementation cancels the AsyncTask when the user clicks on the BACK button of the mobile device.

Since the task is a long time consuming operation , I want to provide the user an alert(warning) whether the user wants to cancel the operation or not , when the user clicks the BACK button of the mobile device.

I am not getting the BACK button callback on onBackPressed() when progressdialog is displayed & the user clicks on the back button.

Another issue which I faced in Samsung Tablet P6200 & not in Android emulator:

When the progressdialog is displayed & the user touches the screen outside the progressdialog area , the progressdialog dismisses .

How can I stop dismissing the progressdialog ?

Any suggestions/hints for implementing the same will be helpful.

}

3 Answers 3

1

For dismissing the progressdialog First remove

mDialog.setCancelable(true);

from your code then try this code as per your Requirement.

private class DownloadQuestion extends AsyncTask<String, Void, String> 
    {           

        @Override
        protected void onPreExecute() 
        {

            pd = ProgressDialog.show(Activity_SplashScreen.this, "","Please wait. Loading latest questions...", true,false);                    

            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... urls) 
        {
            //Write background code here Code 
            return "";
        }

        @Override
        protected void onPostExecute(String response1) 
        {
            Intent intent = new Intent(Activity_SplashScreen.this, Activity_Main.class);
            startActivity(intent);
            finish();

            if (pd != null && pd.isShowing())
                pd.dismiss();

        }
    }
1

You can use setCancelable(false) to stop the back button or tapping away from the dialog closing.

If you want to show a warning, then you will need to implement the setOnCancelListener() or setOnDismissListener() to be notified when the user is dismissing. Be sure to cancel your AsyncTask if the user does cancel as well, otherwise even if your dialog is dismissed the AsyncTask is still running

0

If you use setCancelable(false) then tapping outside the dialog will not cancel it.

And you should call ProgressDialog#show() in an AsyncTask, because the ProgressDialog uses an animation, which holds up the thread's EventHandler it is running in. So in this case you need 3 threads total.

(This is really a design bug in Android)

1
  • Thanks for your comment . I am calling the mDialog.show() from onPreExecute() from an AsyncTask . How to display an alert when the user clicks on BACK button ?
    – chiranjib
    Mar 17, 2012 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.