FragmentContainerView

public final class 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:

  • Creates a new instance of the Fragment
  • Calls onInflate
  • Executes a FragmentTransaction to add the Fragment to the appropriate FragmentManager

You can optionally include an android:tag which allows you to use 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 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 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.

Summary

Public constructors

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

Do not call this constructor directly.

FragmentContainerView(
    @NonNull Context context,
    @Nullable AttributeSet attrs,
    @NonNull int defStyleAttr
)

Do not call this constructor directly.

Public methods

@Override @NonNull void
addView(
    @NonNull View child,
    @NonNull int index,
    @Nullable ViewGroup.LayoutParams params
)

FragmentContainerView will only allow views returned by a Fragment's onCreateView.

@NonNull @RequiresApi(value = "20") @Override WindowInsets

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

@Override @NonNull void
@Nullable F
<F extends Fragment> getFragment()

This method grabs the Fragment whose view was most recently added to the container.

@NonNull @RequiresApi(value = "20") @Override WindowInsets
@Override @NonNull void
@Override @NonNull void
@Override @NonNull void
removeViewAt(@NonNull int index)
@Override @NonNull void
@Override @NonNull void
removeViews(@NonNull int start, @NonNull int count)
@Override @NonNull void
removeViewsInLayout(@NonNull int start, @NonNull int count)
@Override @NonNull void

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

@Override @NonNull void
@Override @NonNull void

Inherited methods

From class FrameLayout
From class View
@NonNull void
@NonNull void
@NonNull void
@NonNull ViewPropertyAnimator
@NonNull void
@NonNull void
@NonNull void
@NonNull void

This method is deprecated.

@NonNull void
@NonNull boolean
@NonNull boolean
@NonNull boolean
@NonNull boolean
@NonNull boolean
@NonNull boolean
canScrollVertically(@NonNull int direction)
final @NonNull void
@NonNull void
final @NonNull void
@NonNull boolean
@NonNull void
@NonNull void
static @NonNull int
combineMeasuredStates(@NonNull int curState, @NonNull int newState)
@NonNull void
@NonNull WindowInsets
computeSystemWindowInsets(
    @NonNull WindowInsets in,
    @NonNull Rect outLocalInsets
)
@NonNull AccessibilityNodeInfo
@NonNull void
@NonNull void

This method is deprecated.

@NonNull boolean
@NonNull boolean
dispatchNestedFling(
    @NonNull float velocityX,
    @NonNull float velocityY,
    @NonNull boolean consumed
)
@NonNull boolean
dispatchNestedPreFling(@NonNull float velocityX, @NonNull float velocityY)
@NonNull boolean
dispatchNestedPrePerformAccessibilityAction(
    @NonNull int action,
    @NonNull Bundle arguments
)
@NonNull boolean
dispatchNestedPreScroll(
    @NonNull int dx,
    @NonNull int dy,
    @NonNull Array<@NonNull int> consumed,
    @NonNull Array<@NonNull int> offsetInWindow
)
@NonNull boolean
dispatchNestedScroll(
    @NonNull int dxConsumed,
    @NonNull int dyConsumed,
    @NonNull int dxUnconsumed,
    @NonNull int dyUnconsumed,
    @NonNull Array<@NonNull int> offsetInWindow
)
@NonNull boolean
@NonNull void
draw(@NonNull Canvas canvas)
@NonNull void
drawableHotspotChanged(@NonNull float x, @NonNull float y)
final @NonNull T
<T extends View> findViewById(@NonNull int id)
final @NonNull T
<T extends View> findViewWithTag(@NonNull Object tag)
@NonNull void
forceHasOverlappingRendering(@NonNull boolean hasOverlappingRendering)
@NonNull void
@NonNull void
generateDisplayHash(
    @NonNull String hashAlgorithm,
    @NonNull Rect bounds,
    @NonNull Executor executor,
    @NonNull DisplayHashResultCallback callback
)
static @NonNull int
@NonNull View.AccessibilityDelegate
@NonNull int
@NonNull AccessibilityNodeProvider
@NonNull CharSequence
@NonNull int
@NonNull int
@NonNull float