FragmentTransaction.add()。commit()和DialogFragment.show()有什么区别?

时间:2016-03-10 09:17:04

标签: android android-fragments android-alertdialog

要显示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");
            }

我何时应该使用?或两者都相同?

1 个答案:

答案 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()

时重置了一些标志