
This resulted in unpleasant -and erroneous- animations when transitioning between Fragments.įragmentContainerView 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. 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. However, on API levels 17 and below, tLayoutTransition(LayoutTransition) must be called for Fragment transitions to work properly.įragmentContainerView and transition animations
#Android studio fragment example android
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.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.Ī FragmentContainerView declaration that uses a tag attribute Adding a Fragment to FragmentContainerView fragment_container_view_tag).Īdding any other views will result in an IllegalStateException being thrown. When FragmentContainerView.addView(View) is called, the view’s tag is retrieved by calling View.getTag(R.id. 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 tTag(R.id.

Adding a View to FragmentContainerViewįragmentContainerView.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). It also supports the attributes, but offers more Fragment transactions flexibility. What is FragmentContainerView?įragmentContainerView is a custom View that extends FrameLayout, but unlike other ViewGroups, it only accepts Fragment Views.

#Android studio fragment example how to
This article explains what FragmentContainerView is, how to interact with it, and goes through some of the Fragment animation issues it tackles. In addition to correctly handling Fragment transactions under the hood, it has some additional features that coordinate with Fragment behavior.

FragmentContainerView is now the recommended View to use in order to host Fragments instead of the previously common way of using FrameLayouts.
