Android Fragments: FragmentContainerView

FragmentContainerView is now the recommended View to use in order to host Fragments instead of the previously common way of using FrameLayouts. In addition to correctly handling Fragment transactions under the hood, it has some additional features that coordinate with Fragment behavior.
This article explains what FragmentContainerView is, how to interact with it, and goes through some of the Fragment animation issues it tackles.
What is FragmentContainerView?
FragmentContainerView is a custom View that extends FrameLayout, but unlike other ViewGroups, it only accepts Fragment Views. It also supports the <fragment>
attributes, but offers more Fragment transactions flexibility.
Adding a View to FragmentContainerView
FragmentContainerView.addView(view)
should only be called with a view that’s associated with a Fragment, this is a View that’s returned by Fragment.onCreateView(LayoutInflater, ViewGroup, boolean)
.
When a Fragment’s view is being created, a tag is assigned to it in order to associate it with the Fragment instance, this is done by a call to View.setTag(R.id.fragment_container_view_tag, fragment)
. When FragmentContainerView.addView(View)
is called, the view’s tag is retrieved by calling View.getTag(R.id.fragment_container_view_tag)
.
Adding any other views will result in an IllegalStateException being thrown.
Adding a Fragment to FragmentContainerView
Since FragmentContainerView is a subclass of FrameLayout, a Fragment can be added to it in the same way it can be added to a FrameLayout.
Another way of adding a Fragment to a FragmentContainerView is by using the android:name
attribute in the XML layout file.
This internally instantiates a MyFragment instance by using a FragmentStore (which is useful if MyFragment’s constructor takes in arguments), then performs a FragmentTransaction in order to add it to the container.
An important thing to note is that, unlike the <fragment>
tag which doesn’t allow to replace the fragment defined statically in the XML layout file via a FragmentTransaction, FragmentContainerView allows to dynamically replace Fragments (just as you’d do if you used a FrameLayout).
One last way of adding a Fragment to a FragmentContainerView, which is very similar to the approach above, is using the class
attribute in an XML layout file.
class
attributeFragmentContainerView XML attributes
FragmentContainerView supports the same attributes as the fragment tag (<fragment>
).
- id: A mandatory attribute if FragmentContainerFragment is to instantiate a -static- Fragment on inflation, this is the case when the
android:name
orclass
attribute is provided. An IllegalStateException is thrown if an id isn’t found. If however theandroid:name
orclass
attribute is not declared, the id becomes optional (even though it would still make sense to add it in order to perform a FragmentTransaction later when adding a Fragment to the FragmentContainerView programmatically). - tag: An optional attribute that associates a tag with the Fragment. The Fragment can then later be retrieved by calling
FragmentManager.findFragmentByTag(tag)
.
- animateLayoutChanges: When set to
true
, this will cause an UnsupportedOperationException to be thrown on Android API levels 18 or above, as layout animations are disabled for FragmentContainerView on these versions because they can conflict with the animations and transitions set through the Fragment APIs. However, on API levels 17 and below,ViewGroup.setLayoutTransition(LayoutTransition)
must be called for Fragment transitions to work properly.
FragmentContainerView and transition animations
Previously, attempting to customize the enter and exit animations of Fragments caused an issue where the entering Fragment would be underneath the exiting Fragment until it completely exited the screen. This resulted in unpleasant -and erroneous- animations when transitioning between Fragments.
FragmentContainerView fixes this Fragments z-ordering issue by ensuring that Fragments with exit animations are drawn first, which results in Fragments with entering animations to be drawn on top.
Transition animations can still be set up, as previously, by calling one of the overloaded FragmentTransaction.setCustomAnimation()
methods.
Conclusion
FragmentContainerView is now the strongly recommended container to host Fragments, it replaces FrameLayouts while supporting the same attributes as the <fragment>
tag, it also attempts to provide a consistent behavior across Android API levels, which is what many other recent Android APIs have been aiming to achieve.
For more on Fragments, check out the resources below.