fragment/src/androidTest/java/androidx/fragment/app/FragmentLifecycleTest.java
@Test
    @UiThreadTest
    public void saveRemovedFragment() throws Throwable {
        FragmentController fc = FragmentTestUtil.createController(mActivityRule);
        FragmentTestUtil.resume(mActivityRule, fc, null);
        FragmentManager fm = fc.getSupportFragmentManager();

        SaveStateFragment fragment1 = SaveStateFragment.create(1);
        fm.beginTransaction()
                .add(android.R.id.content, fragment1, "1")
                .addToBackStack(null)
                .commit();
        SaveStateFragment fragment2 = SaveStateFragment.create(2);
        fm.beginTransaction()
                .replace(android.R.id.content, fragment2, "2")
                .addToBackStack(null)
                .commit();
        fm.executePendingTransactions();

        Pair<Parcelable, FragmentManagerNonConfig> savedState =
                FragmentTestUtil.destroy(mActivityRule, fc);

        fc = FragmentTestUtil.createController(mActivityRule);
        FragmentTestUtil.resume(mActivityRule, fc, savedState);
        fm = fc.getSupportFragmentManager();
        fragment2 = (SaveStateFragment) fm.findFragmentByTag("2");
        assertNotNull(fragment2);
        assertEquals(2, fragment2.getValue());
        fm.popBackStackImmediate();
        fragment1 = (SaveStateFragment) fm.findFragmentByTag("1");
        assertNotNull(fragment1);
        assertEquals(1, fragment1.getValue());
    }
/**
     * When a fragment has been optimized out, it state should still be saved during
     * save and restore instance state.
     */
When a fragment has been optimized out, it state should still be saved during
save and restore instance state.
When a fragment has been optimized out it state should still be saved during save and restore instance state .
@Test
    @UiThreadTest
    public void saveRemovedFragment() throws Throwable {
        FragmentController fc = FragmentTestUtil.createController(mActivityRule);
        FragmentTestUtil.resume(mActivityRule, fc, null);
        FragmentManager fm = fc.getSupportFragmentManager();
        SaveStateFragment fragment1 = SaveStateFragment.create(1);
        fm.beginTransaction()
                .add(android.R.id.content, fragment1, "1")
                .addToBackStack(null)
                .commit();
        SaveStateFragment fragment2 = SaveStateFragment.create(2);
        fm.beginTransaction()
                .replace(android.R.id.content, fragment2, "2")
                .addToBackStack(null)
                .commit();
        fm.executePendingTransactions();
        Pair<Parcelable, FragmentManagerNonConfig> savedState =
                FragmentTestUtil.destroy(mActivityRule, fc);
        fc = FragmentTestUtil.createController(mActivityRule);
        FragmentTestUtil.resume(mActivityRule, fc, savedState);
        fm = fc.getSupportFragmentManager();
        fragment2 = (SaveStateFragment) fm.findFragmentByTag("2");
        assertNotNull(fragment2);
        assertEquals(2, fragment2.getValue());
        fm.popBackStackImmediate();
        fragment1 = (SaveStateFragment) fm.findFragmentByTag("1");
        assertNotNull(fragment1);
        assertEquals(1, fragment1.getValue());
    }
@ Test @ UiThreadTest public void saveRemovedFragment ( ) throws Throwable { FragmentController fc = FragmentTestUtil . createController ( mActivityRule ) ; FragmentTestUtil . resume ( mActivityRule , fc , null ) ; FragmentManager fm = fc . getSupportFragmentManager ( ) ; SaveStateFragment fragment1 = SaveStateFragment . create ( 1 ) ; fm . beginTransaction ( ) . add ( android . R . id . content , fragment1 , "1" ) . addToBackStack ( null ) . commit ( ) ; SaveStateFragment fragment2 = SaveStateFragment . create ( 2 ) ; fm . beginTransaction ( ) . replace ( android . R . id . content , fragment2 , "2" ) . addToBackStack ( null ) . commit ( ) ; fm . executePendingTransactions ( ) ; Pair < Parcelable , FragmentManagerNonConfig > savedState = FragmentTestUtil . destroy ( mActivityRule , fc ) ; fc = FragmentTestUtil . createController ( mActivityRule ) ; FragmentTestUtil . resume ( mActivityRule , fc , savedState ) ; fm = fc . getSupportFragmentManager ( ) ; fragment2 = ( SaveStateFragment ) fm . findFragmentByTag ( "2" ) ; assertNotNull ( fragment2 ) ; assertEquals ( 2 , fragment2 . getValue ( ) ) ; fm . popBackStackImmediate ( ) ; fragment1 = ( SaveStateFragment ) fm . findFragmentByTag ( "1" ) ; assertNotNull ( fragment1 ) ; assertEquals ( 1 , fragment1 . getValue ( ) ) ; }
When a fragment has been optimized out, it state should still be saved during
 save and restore instance state.
When a fragment has been optimized out it state should still be saved during save and restore instance state .