{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

FragmentContainerView

{% setvar page_path %}androidx/fragment/app/FragmentContainerView.html{% endsetvar %} {% setvar can_switch %}1{% endsetvar %} {% include "reference/_java_switcher2.md" %}

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:

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 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). 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)
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

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

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

@NonNull void
@NonNull WindowInsets
onApplyWindowInsets(@NonNull WindowInsets insets)

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

@NonNull void
@NonNull void
removeView(@NonNull View view)
@NonNull void
removeViewAt(@NonNull int index)
@NonNull void
@NonNull void
removeViews(@NonNull int start, @NonNull int count)
@NonNull void
removeViewsInLayout(@NonNull int start, @NonNull int count)
@NonNull void
setLayoutTransition(@Nullable LayoutTransition transition)

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

@NonNull void

Public constructors

FragmentContainerView

@NonNull
public FragmentContainerView(@NonNull Context context)

FragmentContainerView

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

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

FragmentContainerView

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

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

Public methods

addView

@NonNull
public void addView(
    @NonNull View child,
    @NonNull int index,
    @Nullable LayoutParams params
)

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.

endViewTransition

@NonNull
public void endViewTransition(@NonNull View view)

onApplyWindowInsets

@NonNull
public WindowInsets onApplyWindowInsets(@NonNull WindowInsets insets)

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

removeAllViewsInLayout

@NonNull
public void removeAllViewsInLayout()

removeView

@NonNull
public void removeView(@NonNull View view)

removeViewAt

@NonNull
public void removeViewAt(@NonNull int index)

removeViewInLayout

@NonNull
public void removeViewInLayout(@NonNull View view)

removeViews

@NonNull
public void removeViews(@NonNull int start, @NonNull int count)

removeViewsInLayout

@NonNull
public void removeViewsInLayout(@NonNull int start, @NonNull int count)

setLayoutTransition

@NonNull
public void setLayoutTransition(@Nullable LayoutTransition transition)

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
@Nullable LayoutTransition transition

The LayoutTransition object that will animated changes in layout. A value of null means no transition will run on layout changes.

startViewTransition

@NonNull
public void startViewTransition(@NonNull View view)