fragment/src/androidTest/java/androidx/fragment/app/FragmentLifecycleTest.java
@Test
    @UiThreadTest
    public void childFragmentManagerAttach() throws Throwable {
        FragmentController fc = FragmentController.createController(
                new HostCallbacks(mActivityRule.getActivity()));
        fc.attachHost(null);
        fc.dispatchCreate();

        FragmentManager.FragmentLifecycleCallbacks
                mockLc = mock(FragmentManager.FragmentLifecycleCallbacks.class);
        FragmentManager.FragmentLifecycleCallbacks
                mockRecursiveLc = mock(FragmentManager.FragmentLifecycleCallbacks.class);

        FragmentManager fm = fc.getSupportFragmentManager();
        fm.registerFragmentLifecycleCallbacks(mockLc, false);
        fm.registerFragmentLifecycleCallbacks(mockRecursiveLc, true);

        ChildFragmentManagerFragment fragment = new ChildFragmentManagerFragment();
        fm.beginTransaction()
                .add(android.R.id.content, fragment)
                .commitNow();

        verify(mockLc, times(1)).onFragmentCreated(fm, fragment, null);

        fc.dispatchActivityCreated();

        Fragment childFragment = fragment.getChildFragment();

        verify(mockLc, times(1)).onFragmentActivityCreated(fm, fragment, null);
        verify(mockRecursiveLc, times(1)).onFragmentActivityCreated(fm, fragment, null);
        verify(mockRecursiveLc, times(1)).onFragmentActivityCreated(fm, childFragment, null);

        fc.dispatchStart();

        verify(mockLc, times(1)).onFragmentStarted(fm, fragment);
        verify(mockRecursiveLc, times(1)).onFragmentStarted(fm, fragment);
        verify(mockRecursiveLc, times(1)).onFragmentStarted(fm, childFragment);

        fc.dispatchResume();

        verify(mockLc, times(1)).onFragmentResumed(fm, fragment);
        verify(mockRecursiveLc, times(1)).onFragmentResumed(fm, fragment);
        verify(mockRecursiveLc, times(1)).onFragmentResumed(fm, childFragment);

        // Confirm that the parent fragment received onAttachFragment
        assertTrue("parent fragment did not receive onAttachFragment",
                fragment.mCalledOnAttachFragment);

        fc.dispatchStop();

        verify(mockLc, times(1)).onFragmentStopped(fm, fragment);
        verify(mockRecursiveLc, times(1)).onFragmentStopped(fm, fragment);
        verify(mockRecursiveLc, times(1)).onFragmentStopped(fm, childFragment);

        fc.dispatchDestroy();

        verify(mockLc, times(1)).onFragmentDestroyed(fm, fragment);
        verify(mockRecursiveLc, times(1)).onFragmentDestroyed(fm, fragment);
        verify(mockRecursiveLc, times(1)).onFragmentDestroyed(fm, childFragment);
    }
/**
     * This test confirms that as long as a parent fragment has called super.onCreate,
     * any child fragments added, committed and with transactions executed will be brought
     * to at least the CREATED state by the time the parent fragment receives onCreateView.
     * This means the child fragment will have received onAttach/onCreate.
     */
This test confirms that as long as a parent fragment has called super.onCreate,
any child fragments added, committed and with transactions executed will be brought
to at least the CREATED state by the time the parent fragment receives onCreateView.
This means the child fragment will have received onAttach/onCreate.
This test confirms that as long as a parent fragment has called super . onCreate any child fragments added committed and with transactions executed will be brought to at least the CREATED state by the time the parent fragment receives onCreateView . This means the child fragment will have received onAttach / onCreate .
@Test
    @UiThreadTest
    public void childFragmentManagerAttach() throws Throwable {
        FragmentController fc = FragmentController.createController(
                new HostCallbacks(mActivityRule.getActivity()));
        fc.attachHost(null);
        fc.dispatchCreate();
        FragmentManager.FragmentLifecycleCallbacks
                mockLc = mock(FragmentManager.FragmentLifecycleCallbacks.class);
        FragmentManager.FragmentLifecycleCallbacks
                mockRecursiveLc = mock(FragmentManager.FragmentLifecycleCallbacks.class);
        FragmentManager fm = fc.getSupportFragmentManager();
        fm.registerFragmentLifecycleCallbacks(mockLc, false);
        fm.registerFragmentLifecycleCallbacks(mockRecursiveLc, true);
        ChildFragmentManagerFragment fragment = new ChildFragmentManagerFragment();
        fm.beginTransaction()
                .add(android.R.id.content, fragment)
                .commitNow();
        verify(mockLc, times(1)).onFragmentCreated(fm, fragment, null);
        fc.dispatchActivityCreated();
        Fragment childFragment = fragment.getChildFragment();
        verify(mockLc, times(1)).onFragmentActivityCreated(fm, fragment, null);
        verify(mockRecursiveLc, times(1)).onFragmentActivityCreated(fm, fragment, null);
        verify(mockRecursiveLc, times(1)).onFragmentActivityCreated(fm, childFragment, null);
        fc.dispatchStart();
        verify(mockLc, times(1)).onFragmentStarted(fm, fragment);
        verify(mockRecursiveLc, times(1)).onFragmentStarted(fm, fragment);
        verify(mockRecursiveLc, times(1)).onFragmentStarted(fm, childFragment);
        fc.dispatchResume();
        verify(mockLc, times(1)).onFragmentResumed(fm, fragment);
        verify(mockRecursiveLc, times(1)).onFragmentResumed(fm, fragment);
        verify(mockRecursiveLc, times(1)).onFragmentResumed(fm, childFragment);
        assertTrue("parent fragment did not receive onAttachFragment",
                fragment.mCalledOnAttachFragment);
        fc.dispatchStop();
        verify(mockLc, times(1)).onFragmentStopped(fm, fragment);
        verify(mockRecursiveLc, times(1)).onFragmentStopped(fm, fragment);
        verify(mockRecursiveLc, times(1)).onFragmentStopped(fm, childFragment);
        fc.dispatchDestroy();
        verify(mockLc, times(1)).onFragmentDestroyed(fm, fragment);
        verify(mockRecursiveLc, times(1)).onFragmentDestroyed(fm, fragment);
        verify(mockRecursiveLc, times(1)).onFragmentDestroyed(fm, childFragment);
    }
@ Test @ UiThreadTest public void childFragmentManagerAttach ( ) throws Throwable { FragmentController fc = FragmentController . createController ( new HostCallbacks ( mActivityRule . getActivity ( ) ) ) ; fc . attachHost ( null ) ; fc . dispatchCreate ( ) ; FragmentManager . FragmentLifecycleCallbacks mockLc = mock ( FragmentManager . FragmentLifecycleCallbacks . class ) ; FragmentManager . FragmentLifecycleCallbacks mockRecursiveLc = mock ( FragmentManager . FragmentLifecycleCallbacks . class ) ; FragmentManager fm = fc . getSupportFragmentManager ( ) ; fm . registerFragmentLifecycleCallbacks ( mockLc , false ) ; fm . registerFragmentLifecycleCallbacks ( mockRecursiveLc , true ) ; ChildFragmentManagerFragment fragment = new ChildFragmentManagerFragment ( ) ; fm . beginTransaction ( ) . add ( android . R . id . content , fragment ) . commitNow ( ) ; verify ( mockLc , times ( 1 ) ) . onFragmentCreated ( fm , fragment , null ) ; fc . dispatchActivityCreated ( ) ; Fragment childFragment = fragment . getChildFragment ( ) ; verify ( mockLc , times ( 1 ) ) . onFragmentActivityCreated ( fm , fragment , null ) ; verify ( mockRecursiveLc , times ( 1 ) ) . onFragmentActivityCreated ( fm , fragment , null ) ; verify ( mockRecursiveLc , times ( 1 ) ) . onFragmentActivityCreated ( fm , childFragment , null ) ; fc . dispatchStart ( ) ; verify ( mockLc , times ( 1 ) ) . onFragmentStarted ( fm , fragment ) ; verify ( mockRecursiveLc , times ( 1 ) ) . onFragmentStarted ( fm , fragment ) ; verify ( mockRecursiveLc , times ( 1 ) ) . onFragmentStarted ( fm , childFragment ) ; fc . dispatchResume ( ) ; verify ( mockLc , times ( 1 ) ) . onFragmentResumed ( fm , fragment ) ; verify ( mockRecursiveLc , times ( 1 ) ) . onFragmentResumed ( fm , fragment ) ; verify ( mockRecursiveLc , times ( 1 ) ) . onFragmentResumed ( fm , childFragment ) ; assertTrue ( "parent fragment did not receive onAttachFragment" , fragment . mCalledOnAttachFragment ) ; fc . dispatchStop ( ) ; verify ( mockLc , times ( 1 ) ) . onFragmentStopped ( fm , fragment ) ; verify ( mockRecursiveLc , times ( 1 ) ) . onFragmentStopped ( fm , fragment ) ; verify ( mockRecursiveLc , times ( 1 ) ) . onFragmentStopped ( fm , childFragment ) ; fc . dispatchDestroy ( ) ; verify ( mockLc , times ( 1 ) ) . onFragmentDestroyed ( fm , fragment ) ; verify ( mockRecursiveLc , times ( 1 ) ) . onFragmentDestroyed ( fm , fragment ) ; verify ( mockRecursiveLc , times ( 1 ) ) . onFragmentDestroyed ( fm , childFragment ) ; }
This test confirms that as long as a parent fragment has called super.onCreate,
 any child fragments added, committed and with transactions executed will be brought
 to at least the CREATED state by the time the parent fragment receives onCreateView.
This test confirms that as long as a parent fragment has called super . onCreate any child fragments added committed and with transactions executed will be brought to at least the CREATED state by the time the parent fragment receives onCreateView .
// Confirm that the parent fragment received onAttachFragment