javaandroidandroid-fragmentsfragmentmanager

IllegalArgumentException: No view found for id when using getChildFragmentManager() inside viewpager2


My app structure look like this -

(Main Activity)->(Fragment 1)->(ViewPager-> Fragment A,Fragment B).

I am getting error when i do fragment transaction inside Fragment A or B like this. Note here i am using getChildFragmentManager() for fragment transaction.

NotificationDetails notificationFragment = NotificationDetails.newInstance(list);
                        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
                        transaction.setReorderingAllowed(true);
                        transaction.addToBackStack(null);
                        transaction.replace(R.id.Navigation_Main_Layout, notificationFragment, null);
                        transaction.commit();

java.lang.IllegalArgumentException: No view found for id 0x7f0a005b (com.example.id/Navigation_Main_Layout) for fragment NotificationDetails{eaff8ce} (560448c9-144a-49f8-a4d3-47f491b6a563 id=0x7f0a005b)

When i call fragment transaction like this. Note here i am using getSupportFragmentManager() for fragment transaction.it work perfectly without any issues.

NotificationDetails notificationFragment = NotificationDetails.newInstance(list);
                        FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
                        transaction.setReorderingAllowed(true);
                        transaction.addToBackStack(null);
                        transaction.replace(R.id.Navigation_Main_Layout, notificationFragment, null);
                        transaction.commit();

That's my activity layout. You can see the id exist in layout.

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



    <!--The Main Layout For Content-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <!--Toolbar for actionbar   -->
        <include
            android:id="@+id/Navigation_Drawer_toolbar"
            layout="@layout/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@+id/Navigation_Main_Layout"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

             
        </FrameLayout>

    </LinearLayout>

    

</androidx.drawerlayout.widget.DrawerLayout>

Can anyone tell me why it's working with getActivity().getSupportFragmentManager() and it's not when using getChildFragmentManager(). When i should use getChildFragmentManager() inside viewpager's fragment (child fragments).

I also tried with getParentFragmentManager() but getting same error.


Solution

  • As explained in the Fragment Manager guide, which FragmentManager you should be using depends entirely on where the layout is:

    Ownership of each layout in an Activity with nested fragments

    The important being that a child fragment must be placed within its parent's layout - they are required to be nested within one another, in the same way that the Fragments themselves are nested within one another.

    This is explained in the other picture from the documentation, which explains the relationship between each Fragment and the FragmentManagers:

    Relationship between each level of the hierarchy and the given FragmentManager

    In your case, you tried getChildFragmentManager(), but your R.id.Navigation_Main_Layout is not part of the Fragment's layout, so this crashes.

    Then you tried using getParentFragmentManager(), but your R.id.Navigation_Main_Layout isn't in the parent fragment's layout either (since this is the Fragment 1 that contains your ViewPager).

    Your R.id.Navigation_Main_Layout is in your activity's layout, which means the only valid FragmentManager you can use to put a Fragment in your activity's layout (but not inside any fragment), is the Activity's getSupportFragmentManager().