FragmentContainerView

FragmentContainerView is a customized Layout designed specifically for Fragments. It extends FrameLayout, so it can reliably handle Fragment Transactions, and it also has additional features to coordinate with fragment behavior.

FragmentContainerView should be used as the container for Fragments, commonly set in the xml layout of an activity, e.g.:

<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_container_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.fragment.app.FragmentContainerView>

FragmentContainerView can also be used to add a Fragment by using the android:name attribute. FragmentContainerView will perform a one time operation that:

  1. Creates a new instance of the Fragment

  2. Calls Fragment.onInflate

  3. Executes a FragmentTransaction to add the Fragment to the appropriate FragmentManager

You can optionally include an android:tag which allows you to use FragmentManager.findFragmentByTag to retrieve the added Fragment.

<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_container_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.MyFragment"
android:tag="my_tag">
</androidx.fragment.app.FragmentContainerView>

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. Attempting to add any other view will result in an IllegalStateException.

Layout animations and transitions are disabled for FragmentContainerView for APIs above 17. Otherwise, Animations should be done through FragmentTransaction.setCustomAnimations. If animateLayoutChanges is set to true or setLayoutTransition is called directly an UnsupportedOperationException will be thrown.

Fragments using exit animations are drawn before all others for FragmentContainerView. This ensures that exiting Fragments do not appear on top of the view.

Constructors

Link copied to clipboard
constructor(context: Context)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int = 0)

Do not call this constructor directly. Doing so will result in an UnsupportedOperationException.

Functions

Link copied to clipboard
open override fun addView(child: View, index: Int, params: ViewGroup.LayoutParams?)

FragmentContainerView will only allow views returned by a Fragment's Fragment.onCreateView. Attempting to add any other view will result in an IllegalStateException.

Link copied to clipboard
@RequiresApi(value = 20)
open override fun dispatchApplyWindowInsets(insets: WindowInsets): WindowInsets

{@inheritDoc}

Link copied to clipboard
open override fun endViewTransition(view: View)
Link copied to clipboard
fun <F : Fragment?> getFragment(): F

This method grabs the Fragment whose view was most recently added to the container. This may used as an alternative to calling FragmentManager.findFragmentById and passing in the FragmentContainerView's id.

Link copied to clipboard
@RequiresApi(value = 20)
open override fun onApplyWindowInsets(insets: WindowInsets): WindowInsets
Link copied to clipboard
open override fun removeAllViewsInLayout()
Link copied to clipboard
open override fun removeView(view: View)
Link copied to clipboard
open override fun removeViewAt(index: Int)
Link copied to clipboard
open override fun removeViewInLayout(view: View)
Link copied to clipboard
open override fun removeViews(start: Int, count: Int)
Link copied to clipboard
open override fun removeViewsInLayout(start: Int, count: Int)
Link copied to clipboard
open override fun setLayoutTransition(transition: LayoutTransition?)

When called, this method throws a UnsupportedOperationException on APIs above 17. On APIs 17 and below, it calls FrameLayout.setLayoutTransition. This can be called either explicitly, or implicitly by setting animateLayoutChanges to true.

Link copied to clipboard
open override fun startViewTransition(view: View)