Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple fragments in one activity or separate activities

I have been reading this article (excellent btw) and it states this

To create different layouts with fragments you can:

Use one activity, which displays two fragments for tablets and on handset devices. In this case you would switch the fragments in the activity whenever necessary. This requires that the fragment is not declared in the layout file as such fragments cannot be removed during runtime.

Use separate activities to host each fragment on a handset. For example, when the tablet UI uses two fragments in an activity, use the same activity for handsets, but supply an alternative layout that includes just one fragment. When you need to switch fragments, start another activity that hosts the other fragment.

The second approach is the most flexible and in general preferable way of using fragments. In this case the main activity checks if the detail fragment is available in the layout. If the detailed fragment is there, the main activity tells the fragment that it should update itself. If the detail fragment is not available, the main activity starts the detailed activity.

It got me thinking as I would have thought the first option would have been better. (I've seen similar text elsewhere on other tutorials)

With the first option you consistently control the state one on class regardless of whether the orientation/device type

With the second there may be another activity involved. This maybe would in my eyes be a bit of a code smell as it means that you end up with if/else statements that may create a separate activity and the logic is spread.

I'm new to android dev so I'm really after opinions here. Am I missing something?

Thanks

like image 907
RNJ Avatar asked Nov 11 '22 04:11

RNJ


1 Answers

Honestly its your choice. Generally when fragments came out it was a way to create multi pane for tablets and single activity fragement for handsets. The convention has changed that handsets and tablets can use them to make more single responsibility views. As long as you follow SOLID/Good OO it should be fine. And just stick with the style you chose for your app.

like image 139
HexBlit Avatar answered Nov 15 '22 05:11

HexBlit