Launching DialogFragment from CrimeFragment

When launching DialogFragment from CrimeFragment, which is correct among the following snippet?

  1. using ChildFragmentManager
    DialogFragment phtoFragment = CrimePhotoFragment.newInstance(mPhotoFile);
    phtoFragment.show(getChildFragmentManager(), DIALOG_PHOTO);

  2. using FragmentManager of the hosting activity
    DialogFragment phtoFragment = CrimePhotoFragment.newInstance(mPhotoFile);
    phtoFragment.show(getActivity().getSupportFragmentManager(), DIALOG_PHOTO);

Good question. I used FragmentManager and did not have an issue. Perhaps @cstewart can shed some light on this

Neither is correct. Like we do in the book, you want to use getFragmentManager() as your fragment manager in this case.

If you want more detail then that, let me know and we can explore the differences a little more.

Thank you for your answer, cstewart.

Do you mean that we should use FragmentManager of CrimeFragment’s host?
In this case, getFragmentManager() is the same as getActivity().getSupportFragment(), right?
If the host is Fragment, getFragmentManager() is Child FragmentManager.

My question was whether DialogFragment should be managed by CrimeFragment’s host or CrimeFragment when launched from CrimeFragment. If both are fine, any difference?? (back stack,…)

You want to default to the closest fragment manager that you have. So, that means you should avoid doing something like getActivity().getSupportFragment() even if it is going to give you the same result as getFragmentManager().

You want to use the child fragment manager when you are nesting a fragment inside of another fragment. So, if you are adding a layout fragment to the CrimeFragment, you would use the child fragment manager. My understanding is that this is not the case with DialogFragments. For a dialog fragment, I always use getFragmentManager().