要显示DialogFragment,我可以使用以下两种方式:
ProgressDialogFragment fragment = (ProgressDialogFragment) getFragmentManager().findFragmentByTag("progress_dialog");
if (fragment == null) {
fragment = ProgressDialogFragment.newInstance();
fragment.setCancelable(false);
getFragmentManager().beginTransaction()
.add(fragment, "progress_dialog")
.commitAllowingStateLoss();
}
或
ProgressDialogFragment fragment = (ProgressDialogFragment) getFragmentManager().findFragmentByTag("progress_dialog");
if (fragment == null) {
fragment.show(getFragmentManager().beginTransaction(), "progress_dialog");
}
我何时应该使用?或两者都相同?
答案 0 :(得分:3)
几乎一样。 show从您提供的transaction
作为参数检索FragmentManager
,并从代码段中看到调用add / commit
public void show(FragmentManager manager, String tag) {
mDismissed = false;
mShownByMe = true;
FragmentTransaction ft = manager.beginTransaction();
ft.add(this, tag);
ft.commit();
}
唯一的区别是他们在多次调用show()