Android:如何防止后退按钮取消 DialogFragment
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10171822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: How to prevent the back button from cancelling a DialogFragment
提问by MattF
I have a Fragment that can create and pop up a DialogFragment, but when I hit the back button, it dismisses the dialog even though I explicitly call setCancelable(false); Is there any way for my DialogFragment to be insensative to the back button?
我有一个可以创建和弹出 DialogFragment 的 Fragment,但是当我点击后退按钮时,即使我明确调用 setCancelable(false); 它也会关闭对话框;有什么办法可以让我的 DialogFragment 对后退按钮不敏感?
public class LoadingDialogFragment extends DialogFragment
{
String title;
String msg;
public LoadingDialogFragment()
{
this.title = "Loading...";
this.msg = "Please wait...";
}
public LoadingDialogFragment(String title, String msg)
{
this.title = title;
this.msg = msg;
}
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState)
{
final ProgressDialog dialog = new ProgressDialog(getActivity());
dialog.setTitle(title);
dialog.setMessage(msg);
dialog.setIndeterminate(true);
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
return dialog;
}
}
I create the DialogFragment from an AsyncTask:
我从 AsyncTask 创建了 DialogFragment:
private class GpsTask extends AsyncTask<String, Integer, Integer>
{
//ProgressDialog dialog;
@Override
protected void onPreExecute()
{
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
DialogFragment newFragment = new LoadingDialogFragment("Gathering Location", "Acquiring GPS lock...");
ft.addToBackStack(null);
newFragment.show(ft, "dialog");
}
@Override
protected Integer doInBackground(String... params)
{
//acquire a GPS lock and grab a few position updates
}
@Override
protected void onProgressUpdate(Integer... input) { }
@Override
protected void onPostExecute(Integer result)
{
getSupportFragmentManager().popBackStackImmediate();
}
}
回答by Lalit Poptani
How about using setCancelable
? Did you try it?
怎么用setCancelable
?你试了吗?
From the Docs -
从文档 -
Control whether the shown Dialog is cancelable. Use this instead of directly calling Dialog.setCancelable(boolean), because DialogFragment needs to change its behavior based on this
控制显示的 Dialog 是否可取消。使用这个而不是直接调用Dialog.setCancelable(boolean),因为DialogFragment需要根据这个改变它的行为
回答by manavo
I'm not at all sure if this'll work with FragmentDialogs, but if the setCancelable didn't work for you, it might be worth having a look at this article: Android: Prompt user to save changes when Back button is pressed
我完全不确定这是否适用于 FragmentDialogs,但如果 setCancelable 对您不起作用,则可能值得查看这篇文章:Android:按下后退按钮时提示用户保存更改
It explains how to detect the back button being pressed. So maybe you can suppress the button press and it'll stop the dialog from closing?
它解释了如何检测被按下的后退按钮。所以也许你可以抑制按下按钮,它会阻止对话框关闭?
回答by Karthikeyan Ve
It may help you.
它可能会帮助你。
newFragment.setCancelable(false);
make changes like above when creating DialogFragment object or in the constructor of Custom DialogFragment as in the sample below.
在创建 DialogFragment 对象时或在自定义 DialogFragment 的构造函数中进行上述更改,如下例所示。
public static CustomClearHourDialog newInstance(Bundle args, IDialogListener listener)
{
CustomClearHourDialog clearHourDialog = new CustomClearHourDialog();
CustomClearHourDialog.listener = listener;
clearHourDialog.setCancelable(false);
return clearHourDialog;
}