在移动片段之前关闭对话框生成器

Dismiss dialog builder before moving fragment

我在片段 1 中有一个 AlertDialog.Builder,如果我不点击它(将其删除),即使调用另一个片段它也会保留。
我怎么能解雇它?没有关闭方法!

您应该为此目的使用 DialogFragment 并覆盖 onCreateDialog(Bundle) 方法,而不是使用常规片段。 DialogFragment 有 dismiss() 方法,所以你可以随时关闭它。 有关详细信息,请参阅 official documentation

AlertDialog.Builder 用于使用 AlertDialog.Builder.html#show 构建实际对话。您应该将此方法的输出存储在一个变量中,并在以后需要时使用它关闭对话框:

builder = new AlertDialog.Builder();
...
AlertDialog dialog = builder.show();
...
dialog.dismiss(); //or dialog.cancel() which will also call your OnCancelListener