I'm using a ProgressDialog
managed as a Fragment
. Even if I set the ProgressDialog
to be non-cancelable, the BACK button will still operate to remove that Fragment
from the stack. My inner class look like this:
public static class ProgressDialogFragment extends DialogFragment {
private DialogStyle dialogStyle;
public static ProgressDialogFragment newInstance(String title, String message) {
ProgressDialogFragment fragment = new ProgressDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
args.putString("message", message);
fragment.setArguments(args);
return fragment;
}
public void setDialogStyle(DialogStyle dialogStyle) {
this.dialogStyle = dialogStyle;
}
@Override
public ProgressDialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
String message = getArguments().getString("message");
ProgressDialog progressDialog = new ProgressDialog(getActivity());
progressDialog.setTitle(title);
progressDialog.setMessage(message);
if(dialogStyle!=null) {
switch (dialogStyle) {
case CANCELABLE:
progressDialog.setCancelable(true);
break;
case NON_CANCELABLE:
progressDialog.setCancelable(false);
break;
}
} else {
progressDialog.setCancelable(false);
}
progressDialog.show();
return progressDialog;
}
}
And then the method I expose is:
public void showProgressDialog(String title, String message, DialogStyle dialogStyle) {
Fragment prev = fragmentManager.findFragmentByTag("progress dialog");
if(prev!=null) {
ft.remove(prev);
}
ft.addToBackStack(null);
DialogFragment newFragment = ProgressDialogFragment.newInstance(title, message);
((ProgressDialogFragment)newFragment).setDialogStyle(dialogStyle);
newFragment.show(fragmentManager, "progress dialog");
}
So the obvious confusion here is that the BACK button removes the ProgressDialog
because it's being managed as a Fragment
. So how can I make it so that the Dialog
is not cancelable?
Seems strange to try something like:
@Override
public void onBackPressed() {
if(fragmentManager.fragmentManager.findFragmentByTag("progress dialog")!=null) {
}
}
So how can I make it so that the Dialog is not cancelable? Android seems to be saying that ProgressDialog should not be used anymore, that instead you should “use a ProgressBar in your layout.” developer.android.com/guide/topics/ui/dialogs.html.
Showing the DialogFragment It is not necessary to manually create a FragmentTransaction to display your DialogFragment . Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.
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. Alternatively, you can use a notification to inform the user of the task's progress.
Instead of ProgressDialog, why don't you try setCancelable(false)
on DialogFragment?
You can also use setCancelable(false)
on ProgressDialog
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