Xazen
Xazen

Reputation: 822

Smartphone needs Activity and tablet DialogFragment

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?

Upvotes: 0

Views: 856

Answers (1)

PJL
PJL

Reputation: 18794

You don't need to have a Fragment and a DialogFragment as a DialogFragment can 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:

  1. On tablet call show on it to show it as a dialog.
  2. On phone, simpley call replace via FragmentTransaction to replace your 'DialogFragment' into the same container as your game fragment.
  3. On phone, create a simple wrapper activity which you can then call via startActivity(ForResult) and have that wrapper call setContentView with simple full screen layout and then add your DialogFragment into 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.

Upvotes: 3

Related Questions