I am developing a simple game. After every round the results will be displayed. In the smartphone version the results will get an own screen (Usually I would use an Activity, which displays a Fragment). In the tablet version the results will be displayed in a dialog. (Usually I would use DialogFragment).
Now I am not sure how to do it correctly. I read a Fragment shouldn’t load another Fragment unless the Fragment is a DialogFragment. But if I use a DialogFragment, I can’t use it to fill the whole screen. (And if it is possible I think that’s actually not the way how it should be done)
I could write a layout “result.xml”, a DialogFragment and an Activity plus Fragment. But this way I have to implements the functions of the results screen twice and as far as I know that is what the Fragment should prevent the developers from.
Is there a best practice for this?
You don’t need to have a
Fragmentand aDialogFragmentas aDialogFragmentcan be shown as a dialog or not as the case may be, see API demos sample (something along the lines of ‘FragmentDialogOrActivity’.OK so a few ways to do this, keep your DialogFragment and then:
showon it to show it as a dialog.replacevia FragmentTransaction to replace your ‘DialogFragment’ into the same container as your game fragment.startActivity(ForResult)and have that wrapper callsetContentViewwith simple full screen layout and then add yourDialogFragmentinto the container as per 2 .As for which is best practice I don’t necessarily adhere to the view that a fragment shouldn’t load a fragment, particularly if they are closely linked and I don’t see any problems with 2 above for your simple app. Otherwise just go with 3 as it makes little difference.