Add Fragment Over ViewPager2 (FragmentManager is already executing transactions)

I am using viewpager2 in my app through MainActivity.

I want to add a new fragment over that viewPager, However,when trying to do so I am getting an error:

Caused by: java.lang.IllegalStateException: FragmentManager is already executing transactions

The main goal is to inflate a fragment so that it can be displayed inside tab layout.

This is the code I am using to add the fragment:

val biggerImageFragment = BiggerImageFragment.newInstance()
val ft = parentFragmentManager.beginTransaction()
ft.add(rootView.fragmentContainerView.id,biggerImageFragment)
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
ft.addToBackStack(null)
ft.commit()

This is the layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:gravity="center"
    android:id="@+id/tests"
    tools:context=".ui.fragments.SettingsFragment">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_centerInParent="true"
        android:layout_marginStart="15dp"
        android:layout_marginTop="35dp"
        android:layout_marginEnd="15dp"
        android:layout_marginBottom="15dp"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/settingsCoverImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:src="@drawable/cover_image_place_holdedr"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/settingsProfileImage"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_gravity="center"
            android:src="@drawable/profile_picture_place_holder"
            app:layout_constraintBottom_toBottomOf="@+id/settingsCoverImage"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/settingsCoverImage" />

        <TextView
            android:id="@+id/settingsUserName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="Username"
            android:textSize="18sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toTopOf="@+id/cardView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/settingsProfileImage" />

        <androidx.cardview.widget.CardView
            android:id="@+id/cardView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:layout_marginTop="50dp"
            android:layout_marginEnd="10dp"
            app:cardBackgroundColor="@color/secondary_color"
            app:cardCornerRadius="20dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/settingsUserName">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="horizontal"
                android:padding="10dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/settingsCoverImage">

                <ImageButton
                    android:id="@+id/settingsFacebookButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/facebook_icon"
                    android:text="Button" />

                <ImageButton
                    android:id="@+id/settingsInstagramButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="25dp"
                    android:background="@drawable/instagram_icon"
                    android:text="Button" />

                <ImageButton
                    android:id="@+id/settingsWebButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_marginStart="25dp"
                    android:background="@drawable/website_icon"
                    android:text="Button" />
            </LinearLayout>
        </androidx.cardview.widget.CardView>

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/fragmentContainerView"
            android:name="com.dapps.misronim.ui.fragments.SettingsFragment"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </androidx.constraintlayout.widget.ConstraintLayout>



</RelativeLayout>

This is MainActivity:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.MainActivity"
    android:orientation="vertical">


    <LinearLayout
        android:id="@+id/mainActivityLoadingLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/main_color_dark"
        android:clickable="false"
        android:gravity="center"
        android:orientation="vertical"
        android:visibility="gone">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ProgressBar
                android:id="@+id/progressBar"
                style="?android:attr/progressBarStyle"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:indeterminateTint="@color/button_color" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginStart="5dp"
                android:gravity="center"
                android:text="Loading..." />
        </LinearLayout>
    </LinearLayout>

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Theme.Misronim.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/mainToolBar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/main_color_dark"
            app:popupTheme="@style/Theme.Misronim.PopupOverlay">

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/toolbarUserImage"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_marginStart="20dp"
                tools:src="@drawable/tool_bar_user_icon" />

            <TextView
                android:id="@+id/toolbarUserEmailTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:textSize="14sp"
                android:textStyle="bold"
                android:text="" />

        </androidx.appcompat.widget.Toolbar>

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/mainTabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabIndicator="@color/white"
            android:background="@color/main_color_dark">

        </com.google.android.material.tabs.TabLayout>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/mainViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <include
        layout="@layout/content_main" />

</LinearLayout>
ViewPager Class:

    class MainViewPagerAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle) :
        FragmentStateAdapter(fragmentManager, lifecycle) {
    
    
        private val fragmentsArray : Array<Fragment> = arrayOf(
            ChatsFragment(),
            SearchFragment(),
            SettingsFragment())
    
        private val fragmentsTitleArray : ArrayList<String> = arrayListOf(
            "Chats",
            "Search",
            "Settings")
    
        override fun getItemCount(): Int {
            return fragmentsArray.size
        }
    
        override fun createFragment(position: Int): Fragment {
            return fragmentsArray[position]
        }
    
        fun getPageTitle(position : Int) : String{
            return fragmentsTitleArray[position]
        }
    
    
    }

MainActivity starts viewpager2:

val viewPagerAdapter = MainViewPagerAdapter(supportFragmentManager,lifecycle)
binding.mainViewPager.adapter = viewPagerAdapter
TabLayoutMediator(binding.mainTabLayout, binding.mainViewPager) { tab, position ->
    tab.text = viewPagerAdapter.getPageTitle(position)
}.attach()

Thank you !

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum