fragment/src/androidTest/java/androidx/fragment/app/FragmentViewTests.java
@Test
    public void replaceExisting() throws Throwable {
        FragmentTestUtil.setContentView(mActivityRule, R.layout.simple_container);
        ViewGroup container = (ViewGroup)
                mActivityRule.getActivity().findViewById(R.id.fragmentContainer);
        final FragmentManager fm = mActivityRule.getActivity().getSupportFragmentManager();
        final StrictViewFragment fragment1 = new StrictViewFragment();
        final StrictViewFragment fragment2 = new StrictViewFragment();
        fm.beginTransaction()
                .add(R.id.fragmentContainer, fragment1, "1")
                .add(R.id.fragmentContainer, fragment2, "2")
                .commit();
        FragmentTestUtil.executePendingTransactions(mActivityRule);

        FragmentTestUtil.assertChildren(container, fragment1, fragment2);

        fm.beginTransaction()
                .replace(R.id.fragmentContainer, fragment1)
                .addToBackStack(null)
                .commit();
        FragmentTestUtil.executePendingTransactions(mActivityRule);

        FragmentTestUtil.assertChildren(container, fragment1);

        fm.popBackStack();
        FragmentTestUtil.executePendingTransactions(mActivityRule);

        final Fragment replacement1 = fm.findFragmentByTag("1");
        final Fragment replacement2 = fm.findFragmentByTag("2");

        assertSame(fragment1, replacement1);
        FragmentTestUtil.assertChildren(container, replacement1, replacement2);
    }
// Replace a fragment that exists with itself
Replace a fragment that exists with itself
Replace a fragment that exists with itself
@Test
    public void replaceExisting() throws Throwable {
        FragmentTestUtil.setContentView(mActivityRule, R.layout.simple_container);
        ViewGroup container = (ViewGroup)
                mActivityRule.getActivity().findViewById(R.id.fragmentContainer);
        final FragmentManager fm = mActivityRule.getActivity().getSupportFragmentManager();
        final StrictViewFragment fragment1 = new StrictViewFragment();
        final StrictViewFragment fragment2 = new StrictViewFragment();
        fm.beginTransaction()
                .add(R.id.fragmentContainer, fragment1, "1")
                .add(R.id.fragmentContainer, fragment2, "2")
                .commit();
        FragmentTestUtil.executePendingTransactions(mActivityRule);
        FragmentTestUtil.assertChildren(container, fragment1, fragment2);
        fm.beginTransaction()
                .replace(R.id.fragmentContainer, fragment1)
                .addToBackStack(null)
                .commit();
        FragmentTestUtil.executePendingTransactions(mActivityRule);
        FragmentTestUtil.assertChildren(container, fragment1);
        fm.popBackStack();
        FragmentTestUtil.executePendingTransactions(mActivityRule);
        final Fragment replacement1 = fm.findFragmentByTag("1");
        final Fragment replacement2 = fm.findFragmentByTag("2");
        assertSame(fragment1, replacement1);
        FragmentTestUtil.assertChildren(container, replacement1, replacement2);
    }
@ Test public void replaceExisting ( ) throws Throwable { FragmentTestUtil . setContentView ( mActivityRule , R . layout . simple_container ) ; ViewGroup container = ( ViewGroup ) mActivityRule . getActivity ( ) . findViewById ( R . id . fragmentContainer ) ; final FragmentManager fm = mActivityRule . getActivity ( ) . getSupportFragmentManager ( ) ; final StrictViewFragment fragment1 = new StrictViewFragment ( ) ; final StrictViewFragment fragment2 = new StrictViewFragment ( ) ; fm . beginTransaction ( ) . add ( R . id . fragmentContainer , fragment1 , "1" ) . add ( R . id . fragmentContainer , fragment2 , "2" ) . commit ( ) ; FragmentTestUtil . executePendingTransactions ( mActivityRule ) ; FragmentTestUtil . assertChildren ( container , fragment1 , fragment2 ) ; fm . beginTransaction ( ) . replace ( R . id . fragmentContainer , fragment1 ) . addToBackStack ( null ) . commit ( ) ; FragmentTestUtil . executePendingTransactions ( mActivityRule ) ; FragmentTestUtil . assertChildren ( container , fragment1 ) ; fm . popBackStack ( ) ; FragmentTestUtil . executePendingTransactions ( mActivityRule ) ; final Fragment replacement1 = fm . findFragmentByTag ( "1" ) ; final Fragment replacement2 = fm . findFragmentByTag ( "2" ) ; assertSame ( fragment1 , replacement1 ) ; FragmentTestUtil . assertChildren ( container , replacement1 , replacement2 ) ; }
Replace a fragment that exists with itself
Replace a fragment that exists with itself