fragment/src/androidTest/java/androidx/fragment/app/FragmentLifecycleTest.java
@Test
    @UiThreadTest
    public void targetFragmentRestoreLifecycleStateBackStack() throws Throwable {
        final FragmentController fc1 = FragmentController.createController(
                new HostCallbacks(mActivityRule.getActivity()));

        final FragmentManager fm1 = fc1.getSupportFragmentManager();

        fc1.attachHost(null);
        fc1.dispatchCreate();

        final Fragment target = new TargetFragment();
        fm1.beginTransaction().add(target, "target").commitNow();

        final Fragment referrer = new ReferrerFragment();
        referrer.setTargetFragment(target, 0);

        fm1.beginTransaction()
                .remove(target)
                .add(referrer, "referrer")
                .addToBackStack(null)
                .commit();

        fc1.dispatchActivityCreated();
        fc1.noteStateNotSaved();
        fc1.execPendingActions();
        fc1.dispatchStart();
        fc1.dispatchResume();
        fc1.execPendingActions();

        // Bring the state back down to destroyed, simulating an activity restart
        fc1.dispatchPause();
        final Parcelable savedState = fc1.saveAllState();
        final FragmentManagerNonConfig nonconf = fc1.retainNestedNonConfig();
        fc1.dispatchStop();
        fc1.dispatchDestroy();

        final FragmentController fc2 = FragmentController.createController(
                new HostCallbacks(mActivityRule.getActivity()));
        final FragmentManager fm2 = fc2.getSupportFragmentManager();

        fc2.attachHost(null);
        fc2.restoreAllState(savedState, nonconf);
        fc2.dispatchCreate();

        fc2.dispatchActivityCreated();
        fc2.noteStateNotSaved();
        fc2.execPendingActions();
        fc2.dispatchStart();
        fc2.dispatchResume();
        fc2.execPendingActions();

        // Bring the state back down to destroyed before we finish the test
        fc2.dispatchPause();
        fc2.saveAllState();
        fc2.dispatchStop();
        fc2.dispatchDestroy();
    }
/*
     * Test that target fragments are in a useful state when we restore them, even if they're
     * on the back stack.
     */
Test that target fragments are in a useful state when we restore them, even if they're
on the back stack.
Test that target fragments are in a useful state when we restore them even if they ' re on the back stack .
@Test
    @UiThreadTest
    public void targetFragmentRestoreLifecycleStateBackStack() throws Throwable {
        final FragmentController fc1 = FragmentController.createController(
                new HostCallbacks(mActivityRule.getActivity()));
        final FragmentManager fm1 = fc1.getSupportFragmentManager();
        fc1.attachHost(null);
        fc1.dispatchCreate();
        final Fragment target = new TargetFragment();
        fm1.beginTransaction().add(target, "target").commitNow();
        final Fragment referrer = new ReferrerFragment();
        referrer.setTargetFragment(target, 0);
        fm1.beginTransaction()
                .remove(target)
                .add(referrer, "referrer")
                .addToBackStack(null)
                .commit();
        fc1.dispatchActivityCreated();
        fc1.noteStateNotSaved();
        fc1.execPendingActions();
        fc1.dispatchStart();
        fc1.dispatchResume();
        fc1.execPendingActions();
        fc1.dispatchPause();
        final Parcelable savedState = fc1.saveAllState();
        final FragmentManagerNonConfig nonconf = fc1.retainNestedNonConfig();
        fc1.dispatchStop();
        fc1.dispatchDestroy();
        final FragmentController fc2 = FragmentController.createController(
                new HostCallbacks(mActivityRule.getActivity()));
        final FragmentManager fm2 = fc2.getSupportFragmentManager();
        fc2.attachHost(null);
        fc2.restoreAllState(savedState, nonconf);
        fc2.dispatchCreate();
        fc2.dispatchActivityCreated();
        fc2.noteStateNotSaved();
        fc2.execPendingActions();
        fc2.dispatchStart();
        fc2.dispatchResume();
        fc2.execPendingActions();
        fc2.dispatchPause();
        fc2.saveAllState();
        fc2.dispatchStop();
        fc2.dispatchDestroy();
    }
@ Test @ UiThreadTest public void targetFragmentRestoreLifecycleStateBackStack ( ) throws Throwable { final FragmentController fc1 = FragmentController . createController ( new HostCallbacks ( mActivityRule . getActivity ( ) ) ) ; final FragmentManager fm1 = fc1 . getSupportFragmentManager ( ) ; fc1 . attachHost ( null ) ; fc1 . dispatchCreate ( ) ; final Fragment target = new TargetFragment ( ) ; fm1 . beginTransaction ( ) . add ( target , "target" ) . commitNow ( ) ; final Fragment referrer = new ReferrerFragment ( ) ; referrer . setTargetFragment ( target , 0 ) ; fm1 . beginTransaction ( ) . remove ( target ) . add ( referrer , "referrer" ) . addToBackStack ( null ) . commit ( ) ; fc1 . dispatchActivityCreated ( ) ; fc1 . noteStateNotSaved ( ) ; fc1 . execPendingActions ( ) ; fc1 . dispatchStart ( ) ; fc1 . dispatchResume ( ) ; fc1 . execPendingActions ( ) ; fc1 . dispatchPause ( ) ; final Parcelable savedState = fc1 . saveAllState ( ) ; final FragmentManagerNonConfig nonconf = fc1 . retainNestedNonConfig ( ) ; fc1 . dispatchStop ( ) ; fc1 . dispatchDestroy ( ) ; final FragmentController fc2 = FragmentController . createController ( new HostCallbacks ( mActivityRule . getActivity ( ) ) ) ; final FragmentManager fm2 = fc2 . getSupportFragmentManager ( ) ; fc2 . attachHost ( null ) ; fc2 . restoreAllState ( savedState , nonconf ) ; fc2 . dispatchCreate ( ) ; fc2 . dispatchActivityCreated ( ) ; fc2 . noteStateNotSaved ( ) ; fc2 . execPendingActions ( ) ; fc2 . dispatchStart ( ) ; fc2 . dispatchResume ( ) ; fc2 . execPendingActions ( ) ; fc2 . dispatchPause ( ) ; fc2 . saveAllState ( ) ; fc2 . dispatchStop ( ) ; fc2 . dispatchDestroy ( ) ; }
Test that target fragments are in a useful state when we restore them, even if they're
 on the back stack.
Test that target fragments are in a useful state when we restore them even if they ' re on the back stack .
// Bring the state back down to destroyed, simulating an activity restart // Bring the state back down to destroyed before we finish the test