I am found it confusing to be able to support multi screens using fragments. I thought the idea was that i could offer more than one version of the activity and that activity could have 1 or more fragments (depending on real estate) statically embedded.
Supporting dynamic fragments where these are added from code rather than embedded <fragment>
element types. How would this be done in code as I wouldn't know the configuration details of the xml from the activity.
Does anyone have an example of this ?
When would I use static or dynamic fragments.
One reason I could foresee using dynamic fragments is to support the retaining on the fragment so the activity could just ask for it in code but I am finding it difficult to understand how this would be supported without the activity (java) knowing exactly what xml files are available
Thanks
I found it very useful to look through and understand the code of the Android Samples.
These sample projects are usually quite small and focus on only 1 or 2 main concepts each.
If you don't want to actually download and install the sample projects, you can also just browse the source code on GitHub.
For example in the NavigationDrawer sample the NavigationDrawerActivity class contains a dynamically created PlanetFragment class.
There are 2 main reasons to use a "dynamic" fragment:
- You can pass parameters by code. For example, if you have a Fragment that displays an article, you create the Fragment by code and initialize it with the Article to display (using
Fragment.setArguments(Bundle)
). This is the equivalent of passing parameters to an Activity using Intent.putExtras()
.
- You can interchange different fragments in the same container.
Example 1: in your main Activity you have a navigation menu with 3 sections. When the user chooses a new section, you replace Fragment "Section A" with Fragment "Section B" in the same FrameLayout container.
Example 2: You display a two-pane layout on tablet (items list + details). Each time the user selects an item in the left list, you create a new Fragment initialized for the new item to display and replace the existing Fragment on the right with the new one.
Example 3: You have a ViewPager which displays a long list of articles. You swipe right or left to navigate to the previous/next article. These fragments will be created programmatically inside a FragmentPagerAdapter; you can't do that using static Fragments.
To answer your question on how to implement the dual pane with dynamic fragments, here is how:
Create a folder named values-land in your resources. In this folder, add a config.XML file in which you will have a code like this:
<resources>
<item type="bool" name="dual_pane">true</item>
</resources>
In the "normal" values folder, in the config.xml file, add this code:
<resources>
<item type="bool" name="dual_pane">false</item>
</resources>
In your activity, you can detect in the onCreate() the boolean value corresponding to landscape config you just created:
getResources().getBoolean(R.bool.dual_pane);
Then based on the boolean value, you can decide to inflate a layout with just two fragment containers on top of the other (single pane layout) or with two fragment containers next to each other (dual pane layout).
Finally add your fragments dynamically to the appropriate container and voila.
I like this approch because it is very Android like, by the use of the different values folder. Also you don't have to change much to an existing working Fragment pattern to make it support dual pane mode.