fragment/src/androidTest/java/androidx/fragment/app/FragmentViewTests.java
@Test
    public void popToNonBackStackFragment() throws Throwable {
        FragmentTestUtil.setContentView(mActivityRule, R.layout.simple_container);
        final FragmentManager fm = mActivityRule.getActivity().getSupportFragmentManager();

        final SimpleViewFragment fragment1 = new SimpleViewFragment();

        fm.beginTransaction()
                .add(R.id.fragmentContainer, fragment1)
                .commit();

        FragmentTestUtil.executePendingTransactions(mActivityRule);

        final SimpleViewFragment fragment2 = new SimpleViewFragment();

        fm.beginTransaction()
                .replace(R.id.fragmentContainer, fragment2)
                .addToBackStack("two")
                .commit();

        FragmentTestUtil.executePendingTransactions(mActivityRule);

        final SimpleViewFragment fragment3 = new SimpleViewFragment();

        fm.beginTransaction()
                .replace(R.id.fragmentContainer, fragment3)
                .addToBackStack("three")
                .commit();

        FragmentTestUtil.executePendingTransactions(mActivityRule);

        assertEquals(1, fragment1.onCreateViewCount);
        assertEquals(1, fragment2.onCreateViewCount);
        assertEquals(1, fragment3.onCreateViewCount);

        FragmentTestUtil.popBackStackImmediate(mActivityRule, "two",
                FragmentManager.POP_BACK_STACK_INCLUSIVE);

        ViewGroup container = (ViewGroup)
                mActivityRule.getActivity().findViewById(R.id.fragmentContainer);

        FragmentTestUtil.assertChildren(container, fragment1);

        assertEquals(2, fragment1.onCreateViewCount);
        assertEquals(1, fragment2.onCreateViewCount);
        assertEquals(1, fragment3.onCreateViewCount);
    }
// When a non-backstack fragment will be raised, it should not be destroyed.
When a non-backstack fragment will be raised, it should not be destroyed.
When a non - backstack fragment will be raised it should not be destroyed .
@Test
    public void popToNonBackStackFragment() throws Throwable {
        FragmentTestUtil.setContentView(mActivityRule, R.layout.simple_container);
        final FragmentManager fm = mActivityRule.getActivity().getSupportFragmentManager();
        final SimpleViewFragment fragment1 = new SimpleViewFragment();
        fm.beginTransaction()
                .add(R.id.fragmentContainer, fragment1)
                .commit();
        FragmentTestUtil.executePendingTransactions(mActivityRule);
        final SimpleViewFragment fragment2 = new SimpleViewFragment();
        fm.beginTransaction()
                .replace(R.id.fragmentContainer, fragment2)
                .addToBackStack("two")
                .commit();
        FragmentTestUtil.executePendingTransactions(mActivityRule);
        final SimpleViewFragment fragment3 = new SimpleViewFragment();
        fm.beginTransaction()
                .replace(R.id.fragmentContainer, fragment3)
                .addToBackStack("three")
                .commit();
        FragmentTestUtil.executePendingTransactions(mActivityRule);
        assertEquals(1, fragment1.onCreateViewCount);
        assertEquals(1, fragment2.onCreateViewCount);
        assertEquals(1, fragment3.onCreateViewCount);
        FragmentTestUtil.popBackStackImmediate(mActivityRule, "two",
                FragmentManager.POP_BACK_STACK_INCLUSIVE);
        ViewGroup container = (ViewGroup)
                mActivityRule.getActivity().findViewById(R.id.fragmentContainer);
        FragmentTestUtil.assertChildren(container, fragment1);
        assertEquals(2, fragment1.onCreateViewCount);
        assertEquals(1, fragment2.onCreateViewCount);
        assertEquals(1, fragment3.onCreateViewCount);
    }
@ Test public void popToNonBackStackFragment ( ) throws Throwable { FragmentTestUtil . setContentView ( mActivityRule , R . layout . simple_container ) ; final FragmentManager fm = mActivityRule . getActivity ( ) . getSupportFragmentManager ( ) ; final SimpleViewFragment fragment1 = new SimpleViewFragment ( ) ; fm . beginTransaction ( ) . add ( R . id . fragmentContainer , fragment1 ) . commit ( ) ; FragmentTestUtil . executePendingTransactions ( mActivityRule ) ; final SimpleViewFragment fragment2 = new SimpleViewFragment ( ) ; fm . beginTransaction ( ) . replace ( R . id . fragmentContainer , fragment2 ) . addToBackStack ( "two" ) . commit ( ) ; FragmentTestUtil . executePendingTransactions ( mActivityRule ) ; final SimpleViewFragment fragment3 = new SimpleViewFragment ( ) ; fm . beginTransaction ( ) . replace ( R . id . fragmentContainer , fragment3 ) . addToBackStack ( "three" ) . commit ( ) ; FragmentTestUtil . executePendingTransactions ( mActivityRule ) ; assertEquals ( 1 , fragment1 . onCreateViewCount ) ; assertEquals ( 1 , fragment2 . onCreateViewCount ) ; assertEquals ( 1 , fragment3 . onCreateViewCount ) ; FragmentTestUtil . popBackStackImmediate ( mActivityRule , "two" , FragmentManager . POP_BACK_STACK_INCLUSIVE ) ; ViewGroup container = ( ViewGroup ) mActivityRule . getActivity ( ) . findViewById ( R . id . fragmentContainer ) ; FragmentTestUtil . assertChildren ( container , fragment1 ) ; assertEquals ( 2 , fragment1 . onCreateViewCount ) ; assertEquals ( 1 , fragment2 . onCreateViewCount ) ; assertEquals ( 1 , fragment3 . onCreateViewCount ) ; }
When a non-backstack fragment will be raised, it should not be destroyed.
When a non - backstack fragment will be raised it should not be destroyed .