Why is my ProgressDialog listening to ANY KEY (touch) instead of reclining to reject it?

I have a ProgressDialog implemented as follows:

// 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 AsyncTask (PreExecute), so the cancel (true) method stops AsyncTask. All of this works great.

Problem is that I can undo the ProgressDialog with any accidental click on my screen. I want to reject the dialog only by clicking on it. Please help me! Thanks guys.

+6
source share
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); } 

setCanceledOnTouchOutside suggested by GedankenNebel is pretty clean.

+7
source

try the command

not sure if all buttons cancel ... I heard that messages about the onCancel () method are not working properly. my solution is only to make a regular button in the call-back dialog box whenever the button is clicked.

 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 the simple call method from another place in your activity.

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

a fairly simple solution, but it does the trick;) it’s also easy to note that cancelDialog is an action clearing variable, if you do not need to call it from another place, then you should leave by simply limiting the scope of the variable to this method.

+1
source

Source: https://habr.com/ru/post/913919/


All Articles