What is FragmentContainerView? Its benefits over FrameLayout.

Karsh Soni
3 min readJul 5, 2021

If you’re reading this, it means you are an android developer and have interacted with Fragments / FrameLayout. If you have not used FragmentContainerView then you must be using FrameLayout in order to transact / navigate among fragments.

Earlier we used to use FrameLayout in order to host fragments but not with latest updates FragmentContainerView is the recommended view for the same.

In this article i’ll be demonstrating the use of FragmentContainerView and FrameLayout. Why android came up with FragmentContainerView and what were the issues with FrameLayout?

Two major points which FragmentContainerView has improved over FrameLayout.

  1. Fragment transactions made easy as Earlier with FrameLayout, you need transact fragment on your own. Now, it is getting handled by FragmentContainerView under the hood. where the entering Fragment would be underneath the exiting Fragment until it completely exited the screen
  2. Earlier when you customize animations of fragments for enter or exit animations there was some Z-index ordering issue was present where the entering Fragment would be underneath the exiting Fragment until it completely exited the screen and that comes out as a UI/UX issue when transacting between fragments.
Point 1 describes this

What is FragmentContainerView?

  • It is google’s recommended view for hosting fragments.
  • It extends FrameLayout.
  • Adding any other views in FragmentContainerView other than Fragments would throw IllegalStateException.

Ways to add fragments to FragmentContainerView

  • As it is a subclass of FrameLayout you can add fragments using conventional way
Point 1 describes this
  • android:name -> attribute in XML
    android:name=”com.example.YourFragment”
    - Creates a new instance of the Fragment
    - Calls Fragment.onInflate(Context, AttributeSet, Bundle)
    - Executes a FragmentTransaction to add the Fragment to the appropriate FragmentManager
  • You can add android:tag attribute(Optional) to find fragment by tag.
    FragmentManager.findFragmentByTag(String) to retrieve the added Fragment.
  • FragmentContainerView should not be used as a replacement for other ViewGroups (FrameLayout, LinearLayout, etc) outside of Fragment use cases.
  • FragmentContainerView will only allow views returned by a Fragment’s Fragment.onCreateView(LayoutInflater, ViewGroup, Bundle). Attempting to add any other view will result in an IllegalStateException.
  • Fragments using exit animations are drawn before all others for FragmentContainerView. This ensures that exiting Fragments do not appear on top of the view.
Right -> FragmentContainerView Left -> FrameLayout

To demonstrate the difference or the custom animation issue which is now resolved in FragmentContainerView i have created a Demo on GitHub and demo on youtube video. In bot the example i have set same exit and enter animation to demonstrate this.

FrameLayout Fragment Transaction With Custom Animation

FragmentContainerView Fragment Transaction With Custom Animation

Attributes of FragmentContainerView

  • The android:name attribute used to add a default Fragment in the FragmentContainerView container.
  • The app:defaultNavHost=”true” is simply stating that you want this to be the NavHost that intercepts and works as the back button on your device.
  • android:id -> It’s mandatory in case of android:name attribute provided to inflate fragment statically from XML but if you add dynamically and not using andorid:name then android:id becomes optional.
  • NavHostFragments register their navigation controller at the root of their view subtree such that any descendant can obtain the controller instance through the Navigation helper class’s methods such as Navigation.findNavController.
  • app:navGraph=”@navigation/nav_home” -> Contains navigation graph which has actions attached among fragments.
  • Fragments referenced or added via attributes class or android:name on the FragmentContainerView are not kept by ProGuard automatically, so we need to manually add a keep rule for each fragment class.
  • While adding a NavHostFragment via attributes class or android:name in layout file with FragmentContainerView, we cannot use findNavController() in onCreate() of our Activity.

Summary

FragmentContainerView is the recommended view by Google to host fragments.
To get a better view at it practically.

Visit Demo -> Github Youtube
Consult us
@TechnobugsAI

--

--

Karsh Soni

Mobile App Developer | Business - TechnobugsAI | Youtube - CodeHustlers | Writer | Philomath