Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between FragmentTransaction.add().commit() and DialogFragment.show()?

To show DialogFragment, I can use below two ways:

        ProgressDialogFragment fragment = (ProgressDialogFragment) getFragmentManager().findFragmentByTag("progress_dialog");
        if (fragment == null) {
            fragment = ProgressDialogFragment.newInstance();
            fragment.setCancelable(false);
            getFragmentManager().beginTransaction()
                    .add(fragment, "progress_dialog")
                    .commitAllowingStateLoss();

        }

or

            ProgressDialogFragment fragment = (ProgressDialogFragment) getFragmentManager().findFragmentByTag("progress_dialog");
            if (fragment == null) {
fragment.show(getFragmentManager().beginTransaction(), "progress_dialog");
            }

When and which I should use? Or both are same?

like image 943
Victor S Avatar asked Oct 28 '25 15:10

Victor S


1 Answers

there are pretty much the same. show retrieve a transaction, from the FragmentManager you provide as parameter, and call add/commit as you can see from the snippet

   public void show(FragmentManager manager, String tag) {
        mDismissed = false;
        mShownByMe = true;
        FragmentTransaction ft = manager.beginTransaction();
        ft.add(this, tag);
        ft.commit();
    }

the only difference is they reset some flags upon multiple calls of show()

like image 84
Blackbelt Avatar answered Oct 31 '25 13:10

Blackbelt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!