我正在使用作为Fragment
的ProgressDialog
。即使我将ProgressDialog
设置为不可取消,后退按钮仍将从堆栈中删除该Fragment
。我的内部类看起来像这样:
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;
}
}
然后我公开的方法是:
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");
}
这里明显的困惑在于,后退按钮会移除ProgressDialog
,因为它被视为一个Fragment
来管理。那么,我该如何使Dialog
不可取消呢?
尝试下面这个方法似乎有点奇怪:
@Override
public void onBackPressed() {
if(fragmentManager.fragmentManager.findFragmentByTag("progress dialog")!=null) {
}
}