Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my ProgressDialog listening on ANY KEY (touch) instead of backbutton to dismiss?

i have a ProgressDialog implemented like this:

// show progress dialog while date is loading
        progressDialog = ProgressDialog.show(XYActivity.this, getResources().getString(R.string.progress_dialog_please_wait), getResources().getString(R.string.progress_dialog_loading), true);
        progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                cancel(true);
                Log.w(LOGTAG, "loading cancelled via back button");
            }

        });
        progressDialog.setCancelable(true);

This ProgressDialog is implemented inside an AsyncTask (PreExecute), so the cancel(true) method stops the AsyncTask. This all works fine.

The problem is, that i can cancel the ProgressDialog with any random touch on my screen. I wanna to dismiss the dialog only by pressing the backbutton. Please help me! Thank you guys.

like image 442
GedankenNebel Avatar asked Apr 23 '12 11:04

GedankenNebel


People also ask

What can I use instead of progress dialog?

"Deprecated" refers to functions or elements that are in the process of being replaced by newer ones. ProgressDialog is a modal dialog, which prevents the user from interacting with the app. Instead of using this class, you should use a progress indicator like ProgressBar , which can be embedded in your app's UI.

Can you have an alert dialog without any buttons if not why?

You can do this very easily. AlertDialog. Builder alertDialogBuilder = new AlertDialog. Builder(context); // set title alertDialogBuilder.


2 Answers

This worked for me:

@Override
protected void onPreExecute() {
    progressDialog = ProgressDialog.show(context, "Title", "Loading...", true, true, new OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            cancel(true);
        }
    });
    progressDialog.setCanceledOnTouchOutside(false);
}

The setCanceledOnTouchOutside suggested by GedankenNebel is pretty clean.

like image 106
TMT020 Avatar answered Oct 21 '22 07:10

TMT020


try below instruction

not sure about the whole cancel button...i've heard reports of the onCancel() method not firing properly. my solution just consists of making a normal button on the dialog with a call to return whenever the button is pressed.

private void createCancelProgressDialog(String title, String message, String buttonText)
{
    cancelDialog = new ProgressDialog(this);
    cancelDialog.setTitle(title);
    cancelDialog.setMessage(message);
    cancelDialog.setButton(buttonText, new DialogInterface.OnClickListener() 
    {
        public void onClick(DialogInterface dialog, int which) 
        {
            // Use either finish() or return() to either close the activity or just the dialog
            cancelDialog.dismiss();
        }
    });
    cancelDialog.show();
}

then just use a simple call method from elsewhere in your activity

createCancelProgressDialog("Loading", "Please wait while activity is loading", "Cancel");

rather simple solution, but it does the trick ;) also just to note that cancelDialog is an activity wipe variable, if you dont need to call it from elsewhere, then you should be able to get away with just limiting the scope of the variable to that method.

like image 1
Dinesh Avatar answered Oct 21 '22 06:10

Dinesh