我见过有些人使用下面的代码。初始化并调用片段。
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
Fragment prev = manager.findFragmentByTag("yourTag");
if (prev != null) {
ft.remove(prev);
}
DialogFragment newFragment = YourFragmentDialog.newInstance();
newFragment.show(ft, "yourTag");
DialogFragment newFragment = YourFragmentDialog.newInstance();
newFragment.show(getFragmentManager(), "yourTag");
我有3个问题:
DialogFragment
后。是自动收集垃圾还是我们应该将其明确设置为null
?答案 0 :(得分:0)
您可以使用以下方法显示正确删除的DialogFragment
&从官方doc替换当前显示的任何新对话框。您可以通过它来了解实现它的正确方法。使用getSupportFragmentManager()
以获得更好的效果。请遵循官方的规定避免这些类型的混淆文档。
void showDialog() {
// DialogFragment.show() will take care of adding the fragment
// in a transaction. We also want to remove any currently showing
// dialog, so make our own transaction and take care of that here.
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
// Create and show the dialog.
DialogFragment newFragment = MyDialogFragment.newInstance();
newFragment.show(ft, "dialog");
}