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.
"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.
You can do this very easily. AlertDialog. Builder alertDialogBuilder = new AlertDialog. Builder(context); // set title alertDialogBuilder.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With