gpt4 book ai didi

android - 防止后退按钮取消 DialogFragment

转载 作者:IT王子 更新时间:2023-10-28 23:48:08 25 4
gpt4 key购买 nike

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

最佳答案

如何使用setCancelable ?你试过了吗?

来自文档:

Control whether the shown Dialog is cancelable. Use this instead ofdirectly calling Dialog.setCancelable(boolean), because DialogFragmentneeds to change its behavior based on this

用于自定义 DialogFragment

onCreateDialog

处添加 isCancelable = false

关于android - 防止后退按钮取消 DialogFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10171822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com