avatarSatya Pavan Kantamani

Summary

This context provides an overview and guide on using FragmentContainerView in Android, a custom layout designed specifically for dealing with fragments, including its history, usage, and known issues.

Abstract

FragmentContainerView is a custom view designed to optimize fragment transactions and improve the handling of fragments in Android applications. Introduced in Android Dev Summit 2019, FragmentContainerView extends FrameLayout and provides additional features related to fragment behavior. Adding fragments to FragmentContainerView is similar to adding them to FrameLayout. The context covers the process of adding a fragment, the attributes required, and the one-time operation performed by FragmentContainerView. Additionally, the text highlights known issues and limitations of using FragmentContainerView, such as the inability to use animateLayoutChanges or setLayoutTransition, and the need for manual ProGuard keep rules for fragment classes. Despite these limitations, FragmentContainerView is recommended as the container for hosting fragments in Android applications.

Bullet points

  • FragmentContainerView is a custom layout designed specifically for dealing with fragments.
  • FragmentContainerView extends FrameLayout and handles attributes related to fragments.
  • Adding fragments to FragmentContainerView is similar to adding them to FrameLayout.
  • FragmentContainerView performs a one-time operation that creates a new instance of the fragment, calls Fragment.onInflate(Context, AttributeSet, Bundle), and executes a FragmentTransaction to add the fragment to the appropriate FragmentManager.
  • Known issues with FragmentContainerView include the inability to use animateLayoutChanges or setLayoutTransition, the need for manual ProGuard keep rules for fragment classes, and the inability to use findNavController() in onCreate() of an Activity when adding a NavHostFragment via attributes in a layout file with FragmentContainerView.
  • Despite these limitations, FragmentContainerView is recommended as the container for hosting fragments in Android applications.

FragmentContainerView

Learn how to add fragments into FragmentContainerView

Photo by Markus Winkler on Unsplash

History

Usually in Android, before NavGraph if we want to add a fragment dynamically we used to add container layout mostly FrameLayout in the XML and then add the required instance to the container using fragment manager. We used to use different kinds of layouts before but none of these layouts are optimized for fragment transactions or fragments stuff. As the fragments usage has become a common thing in every app to simplify that and to optimize the fragments stuff Android team came up with FragmentContainerView. In this post let’s check out what is FragmentContainerView and how to use it.

What is FragmentContainerView?

FragmentContainerView is basically a custom view designed extending FrameLayout. In simple terms, we can say FragmentContainerView is a custom layout designed specifically for dealing with fragments. As it was extending FramLayout it can reliably handle Fragment Transactions and has some more additional features related to fragment behavior. Unlike other ViewGroups, it only handles attributes related to fragments. So we should not use FragmentContainerView as a replacement for other ViewGroups (FrameLayout, LinearLayout, etc) outside of Fragment use cases.

Attempting to add any other view other than fragments will result in an IllegalStateException.

Pre-requisites

FragmentContainerView first came to limelight in Android Dev Summit 2019. Add the following dependencies to build.gradle file.

  implementation "androidx.fragment:fragment-ktx:1.2.0-rc01"

How to add a Fragment to FragmentContainerView

Adding Fragment to FragmentContainerView has a similar procedure of adding a fragment to FrameLayout.

Another way to simply add a Fragment to FragmentContainerView is by using the attribute android:name=”fragment_class_name_path" in XML. Both the attributes android:name or class are similar things we just need to give the classpath as a value to inflate the fragment.

Here FragmentContainerView will use FragmentTransaction under the hood to create and show our fragment. So that we can use the fragment transactions later to replace it.

As per developer documentation

FragmentContainerView will perform a one-time operation that:

  • Creates a new instance of the Fragment
  • Calls Fragment.onInflate(Context, AttributeSet, Bundle)
  • Executes a FragmentTransaction to add the Fragment to the appropriate FragmentManager

We can optionally include an attribute android:tag which allows us to use FragmentManager.findFragmentByTag(String) to retrieve the added Fragment.

Known Issues

  • If the attribute animateLayoutChanges is set to true or setLayoutTransition(LayoutTransition)is called it will directly throw UnsupportedOperationException as the layout animations and transitions are disabled for FragmentContainerView for APIs above 17.
  • Fragments referenced or added via attributesclass or android:name on theFragmentContainerView 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.

FragmentContainerView simplified adding fragments at the layout file without the need of adding any code in activities. FragmentContainerView is now the strongly recommended container for hosting Fragments. Explore more at the FragmentContainerView docs. Expecting the known issues will be resolved soon as they concern us more.

Please let me know your suggestions and comments.

You can find me on Medium and LinkedIn

Thanks for reading…

Android
Android App Development
AndroidDev
Programming
Android Development
Recommended from ReadMedium