如何以编程方式关闭DialogFragment?我目前是通过以下方式创建对话框的:
void showDialogWithId(int id){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
if (id == SEARCHING_DIALOG){
// Create and show the dialog.
DialogFragment newFragment = MyDialogFragment.newInstance(SEARCHING_DIALOG,"TEST");
newFragment.show(ft, "dialog");
}
if (id == CONNECTING_DIALOG){
// Create and show the dialog.
DialogFragment newFragment = MyDialogFragment.newInstance(CONNECTING_DIALOG,"TEST");
newFragment.show(ft, "dialog");
}
if (id == CONNECTIVITY_DIALOG){
// Create and show the dialog.
DialogFragment newFragment = MyDialogFragment.newInstance(CONNECTIVITY_DIALOG);
newFragment.show(ft, "dialog");
}
}
我想通过以下方式来解散他们:
public void dismissDialog(){
getFragmentManager().popBackStack();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
}
然而,对话框没有被关闭...
getDialog().dismiss()
和dismiss()
两者都会导致Activity结束。请参考:https://dev59.com/V5zga4cB1Zd3GeqP_xrl - JCarlosR