我有一个可以创建和弹出 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;
}
}
我从 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();
}
}