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

        OnCreateFragment fragment1 = new OnCreateFragment();

        fm.beginTransaction()
                .add(fragment1, "1")
                .commit();
        fm.beginTransaction()
                .remove(fragment1)
                .addToBackStack(null)
                .commit();

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

        fc = FragmentTestUtil.createController(mActivityRule);
        FragmentTestUtil.resume(mActivityRule, fc, restartState);

        // Save again, but keep the state
        savedState = FragmentTestUtil.destroy(mActivityRule, fc);

        fc = FragmentTestUtil.createController(mActivityRule);
        FragmentTestUtil.resume(mActivityRule, fc, savedState);

        fm = fc.getSupportFragmentManager();

        fm.popBackStackImmediate();
        OnCreateFragment fragment2 = (OnCreateFragment) fm.findFragmentByTag("1");
        assertTrue(fragment2.onCreateCalled);
        fm.popBackStackImmediate();
    }
/**
     * When a retained instance fragment is saved while in the back stack, it should go
     * through onCreate() when it is popped back.
     */
When a retained instance fragment is saved while in the back stack, it should go
through onCreate() when it is popped back.
When a retained instance fragment is saved while in the back stack it should go through onCreate () when it is popped back .
@Test
    @UiThreadTest
    public void retainInstanceWithOnCreate() throws Throwable {
        FragmentController fc = FragmentTestUtil.createController(mActivityRule);
        FragmentTestUtil.resume(mActivityRule, fc, null);
        FragmentManager fm = fc.getSupportFragmentManager();
        OnCreateFragment fragment1 = new OnCreateFragment();
        fm.beginTransaction()
                .add(fragment1, "1")
                .commit();
        fm.beginTransaction()
                .remove(fragment1)
                .addToBackStack(null)
                .commit();
        Pair<Parcelable, FragmentManagerNonConfig> savedState =
                FragmentTestUtil.destroy(mActivityRule, fc);
        Pair<Parcelable, FragmentManagerNonConfig> restartState =
                Pair.create(savedState.first, null);
        fc = FragmentTestUtil.createController(mActivityRule);
        FragmentTestUtil.resume(mActivityRule, fc, restartState);
        savedState = FragmentTestUtil.destroy(mActivityRule, fc);
        fc = FragmentTestUtil.createController(mActivityRule);
        FragmentTestUtil.resume(mActivityRule, fc, savedState);
        fm = fc.getSupportFragmentManager();
        fm.popBackStackImmediate();
        OnCreateFragment fragment2 = (OnCreateFragment) fm.findFragmentByTag("1");
        assertTrue(fragment2.onCreateCalled);
        fm.popBackStackImmediate();
    }
@ Test @ UiThreadTest public void retainInstanceWithOnCreate ( ) throws Throwable { FragmentController fc = FragmentTestUtil . createController ( mActivityRule ) ; FragmentTestUtil . resume ( mActivityRule , fc , null ) ; FragmentManager fm = fc . getSupportFragmentManager ( ) ; OnCreateFragment fragment1 = new OnCreateFragment ( ) ; fm . beginTransaction ( ) . add ( fragment1 , "1" ) . commit ( ) ; fm . beginTransaction ( ) . remove ( fragment1 ) . addToBackStack ( null ) . commit ( ) ; Pair < Parcelable , FragmentManagerNonConfig > savedState = FragmentTestUtil . destroy ( mActivityRule , fc ) ; Pair < Parcelable , FragmentManagerNonConfig > restartState = Pair . create ( savedState . first , null ) ; fc = FragmentTestUtil . createController ( mActivityRule ) ; FragmentTestUtil . resume ( mActivityRule , fc , restartState ) ; savedState = FragmentTestUtil . destroy ( mActivityRule , fc ) ; fc = FragmentTestUtil . createController ( mActivityRule ) ; FragmentTestUtil . resume ( mActivityRule , fc , savedState ) ; fm = fc . getSupportFragmentManager ( ) ; fm . popBackStackImmediate ( ) ; OnCreateFragment fragment2 = ( OnCreateFragment ) fm . findFragmentByTag ( "1" ) ; assertTrue ( fragment2 . onCreateCalled ) ; fm . popBackStackImmediate ( ) ; }
When a retained instance fragment is saved while in the back stack, it should go
 through onCreate() when it is popped back.
When a retained instance fragment is saved while in the back stack it should go through onCreate () when it is popped back .
// Save again, but keep the state