ViewGroup container is null in onCreateView of a Fragment added with FragmentContainerView

Viewed 286

CONTEXT:

I am using FragmentContainerView in order to add a Fragment in my XML.

I have feedback from testers, when they launch the App from Background state, the Fragment is not displayed.

To witness this bug, I have enabled the developer option "Don't Keep Activites".
Indeed, with this option, the Fragment is displayed properly during the first launch. Then, I put the App in the background, and launch it again -> Fragment is gone.


CODE:

Here is the XML with my FragmentContainerView:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainTileContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragment_main_tile"
        android:name="com.app.fragment.FragmentMainTile"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </androidx.fragment.app.FragmentContainerView>

</FrameLayout>

The Fragment added inside the FragmentContainerView contains a ViewPager with a TabLayout.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/containerContraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/blurredConstraintLayout"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginStart="12dp"
        android:layout_marginTop="12dp"
        android:layout_marginEnd="12dp"
        android:background="@drawable/blurred_background"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tablayout"
            style="@style/MyTabLayout"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:tabGravity="fill"
            app:tabIndicatorHeight="0dp"
            app:tabMode="fixed"
            app:tabRippleColor="@android:color/transparent"
            app:tabSelectedTextColor="#084E92"
            app:tabTextColor="@android:color/white" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/blurredConstraintLayout" />

</androidx.constraintlayout.widget.ConstraintLayout>

With breakpoints inside the onCreateView method of my Fragment, the ViewGroup is my FragmentContainerView for the first launch, and is null after coming from background.

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_main_container, container, false)
    }

I thought the FragmentContainerView would handle the Fragment operation for me :

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

Should I handle save/restore State within my Fragment? Can someone explains this behavior?

0 Answers
Related