我使用了一个 ProgressDialog
管理为片段
。即使我设置 ProgressDialog
是不可取消,返回按钮将仍删除了片段
从叠加。我的内部类是这样的:
公共静态类ProgressDialogFragment扩展DialogFragment {
私人DialogStyle dialogStyle;
公共静态ProgressDialogFragment的newInstance(标题字符串,字符串消息){
ProgressDialogFragment片段=新ProgressDialogFragment();
捆绑的args =新包();
args.putString(题,题);
args.putString(消息,消息);
fragment.setArguments(参数);
返回片段;
}
公共无效setDialogStyle(DialogStyle dialogStyle){
this.dialogStyle = dialogStyle;
}
@覆盖
公共ProgressDialog onCreateDialog(包savedInstanceState){
。字符串title = getArguments()的getString(标题);
字符串消息= getArguments()的getString(信息)。
ProgressDialog progressDialog =新ProgressDialog(getActivity());
progressDialog.setTitle(职称);
progressDialog.setMessage(消息);
如果(dialogStyle!= NULL){
开关(dialogStyle){
情况下取消:
progressDialog.setCancelable(真正的);
打破;
案例NON_CANCELABLE:
progressDialog.setCancelable(假);
打破;
}
} 其他 {
progressDialog.setCancelable(假);
}
progressDialog.show();
返回progressDialog;
}
}
然后我揭露的方法是:
公共无效showProgressDialog(标题字符串,字符串消息,DialogStyle dialogStyle){
片段preV = fragmentManager.findFragmentByTag(进度对话框);
如果(preV!= NULL){
ft.remove(preV);
}
ft.addToBackStack(空);
DialogFragment newFragment = ProgressDialogFragment.newInstance(标题,邮件);
((ProgressDialogFragment)newFragment).setDialogStyle(dialogStyle);
newFragment.show(fragmentManager,进度对话框);
}
因此,这里明显的困惑是,BACK按钮删除 ProgressDialog
,因为它被管理作为一个片段
。那么,如何可以让这个在对话框
不可取消?
觉得奇怪,试着这么做:
@覆盖
公共无效onBack pressed(){
如果(fragmentManager.fragmentManager.findFragmentByTag(进度对话框)!= NULL){
}
}
解决方案
相反ProgressDialog的,你为什么不尝试 setCancelable(假)
上DialogFragment?
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) {
}
}
解决方案
Instead of ProgressDialog, why don't you try setCancelable(false)
on DialogFragment?