The Android Developer Challenge is back! Submit your idea before December 2.

FragmentContainerView

class FragmentContainerView : FrameLayout
kotlin.Any
   ↳ android.view.View
   ↳ android.view.ViewGroup
   ↳ android.widget.FrameLayout
   ↳ androidx.fragment.app.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:

You can optionally include an android:tag which allows you to use FragmentManager#findFragmentByTag(String) 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 to returned by a Fragment's Fragment#onCreateView(LayoutInflater, ViewGroup, Bundle). 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(int, int, int, int). IfanimateLayoutChanges is set to true or setLayoutTransition(LayoutTransition) 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.

Summary

Public constructors

<init>(@NonNull context: Context)

<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?)

Do not call this constructor directly.

<init>(@NonNull context: Context, @Nullable attrs: AttributeSet?, defStyleAttr: Int)

Do not call this constructor directly.

Public methods

Unit
addView(@NonNull child: View, index: Int, @Nullable params: LayoutParams?)

FragmentContainerView will only allow views to returned by a Fragment's Fragment#onCreateView(LayoutInflater, ViewGroup, Bundle).

Unit
endViewTransition(@NonNull view: View)

WindowInsets

The sys ui flags must be set to enable extending the layout into the window insets.

Unit

Unit
removeView(@NonNull view: View)

Unit
removeViewAt(index: Int)

Unit
removeViewInLayout(@NonNull view: View)

Unit
removeViews(start: Int, count: Int)

Unit
removeViewsInLayout(start: Int, count: Int)

Unit
setLayoutTransition(@Nullable transition: LayoutTransition?)

When called, this method throws a UnsupportedOperationException on APIs above 17.

Unit
startViewTransition(@NonNull view: View)

Protected methods

Boolean
addViewInLayout(@NonNull child: View, index: Int, @Nullable params: LayoutParams?, preventRequestLayout: Boolean)

FragmentContainerView will only allow views to returned by a Fragment's Fragment#onCreateView(LayoutInflater, ViewGroup, Bundle).

Unit
dispatchDraw(@NonNull canvas: Canvas)

Boolean
drawChild(@NonNull canvas: Canvas, @NonNull child: View, drawingTime: Long)

Unit
removeDetachedView(@NonNull child: View, animate: Boolean)

Public constructors

<init>

FragmentContainerView(@NonNull context: Context)

<init>

FragmentContainerView(@NonNull context: Context, @Nullable attrs: AttributeSet?)

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

<init>

FragmentContainerView(@NonNull context: Context, @Nullable attrs: AttributeSet?, defStyleAttr: Int)

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

Public methods

addView

fun addView(@NonNull child: View, index: Int, @Nullable params: LayoutParams?): Unit

FragmentContainerView will only allow views to returned by a Fragment's Fragment#onCreateView(LayoutInflater, ViewGroup, Bundle). Attempting to add any other view will result in an IllegalStateException.

endViewTransition

fun endViewTransition(@NonNull view: View): Unit

onApplyWindowInsets

@NonNull fun onApplyWindowInsets(@NonNull insets: WindowInsets): WindowInsets

The sys ui flags must be set to enable extending the layout into the window insets.

removeAllViewsInLayout

fun removeAllViewsInLayout(): Unit

removeView

fun removeView(@NonNull view: View): Unit

removeViewAt

fun removeViewAt(index: Int): Unit

removeViewInLayout

fun removeViewInLayout(@NonNull view: View): Unit

removeViews

fun removeViews(start: Int, count: Int): Unit

removeViewsInLayout

fun removeViewsInLayout(start: Int, count: Int): Unit

setLayoutTransition

fun setLayoutTransition(@Nullable transition: LayoutTransition?): Unit

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

View animations and transitions are disabled for FragmentContainerView for APIs above 17. Use FragmentTransaction#setCustomAnimations(int, int, int, int) and FragmentTransaction#setTransition(int).

Parameters
transition LayoutTransition?: The LayoutTransition object that will animated changes in layout. A value of null means no transition will run on layout changes. android.R.attr#animateLayoutChanges

startViewTransition

fun startViewTransition(@NonNull view: View): Unit

Protected methods

addViewInLayout

protected fun addViewInLayout(@NonNull child: View, index: Int, @Nullable params: LayoutParams?, preventRequestLayout: Boolean): Boolean

FragmentContainerView will only allow views to returned by a Fragment's Fragment#onCreateView(LayoutInflater, ViewGroup, Bundle). Attempting to add any other view will result in an IllegalStateException.

dispatchDraw

protected fun dispatchDraw(@NonNull canvas: Canvas): Unit

drawChild

protected fun drawChild(@NonNull canvas: Canvas, @NonNull child: View, drawingTime: Long): Boolean

removeDetachedView

protected fun removeDetachedView(@NonNull child: View, animate: Boolean): Unit