fragment/src/androidTest/java/androidx/fragment/app/FragmentViewTests.java
@Test
    public void replaceOne() 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();
        fm.beginTransaction().add(R.id.fragmentContainer, fragment1, "1").commit();
        FragmentTestUtil.executePendingTransactions(mActivityRule);

        FragmentTestUtil.assertChildren(container, fragment1);

        final StrictViewFragment fragment2 = new StrictViewFragment();
        fm.beginTransaction()
                .replace(R.id.fragmentContainer, fragment2)
                .addToBackStack(null)
                .commit();
        FragmentTestUtil.executePendingTransactions(mActivityRule);

        FragmentTestUtil.assertChildren(container, fragment2);
        assertEquals(View.VISIBLE, fragment2.getView().getVisibility());

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

        Fragment replacement1 = fm.findFragmentByTag("1");
        assertNotNull(replacement1);
        FragmentTestUtil.assertChildren(container, replacement1);
        assertFalse(replacement1.isHidden());
        assertTrue(replacement1.isAdded());
        assertFalse(replacement1.isDetached());
        assertEquals(View.VISIBLE, replacement1.getView().getVisibility());
    }
// Simple replace of one fragment in a container. Popping should replace it back again
Simple replace of one fragment in a container. Popping should replace it back again
Simple replace of one fragment in a container . Popping should replace it back again
@Test
    public void replaceOne() 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();
        fm.beginTransaction().add(R.id.fragmentContainer, fragment1, "1").commit();
        FragmentTestUtil.executePendingTransactions(mActivityRule);
        FragmentTestUtil.assertChildren(container, fragment1);
        final StrictViewFragment fragment2 = new StrictViewFragment();
        fm.beginTransaction()
                .replace(R.id.fragmentContainer, fragment2)
                .addToBackStack(null)
                .commit();
        FragmentTestUtil.executePendingTransactions(mActivityRule);
        FragmentTestUtil.assertChildren(container, fragment2);
        assertEquals(View.VISIBLE, fragment2.getView().getVisibility());
        fm.popBackStack();
        FragmentTestUtil.executePendingTransactions(mActivityRule);
        Fragment replacement1 = fm.findFragmentByTag("1");
        assertNotNull(replacement1);
        FragmentTestUtil.assertChildren(container, replacement1);
        assertFalse(replacement1.isHidden());
        assertTrue(replacement1.isAdded());
        assertFalse(replacement1.isDetached());
        assertEquals(View.VISIBLE, replacement1.getView().getVisibility());
    }
@ Test public void replaceOne ( ) 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 ( ) ; fm . beginTransaction ( ) . add ( R . id . fragmentContainer , fragment1 , "1" ) . commit ( ) ; FragmentTestUtil . executePendingTransactions ( mActivityRule ) ; FragmentTestUtil . assertChildren ( container , fragment1 ) ; final StrictViewFragment fragment2 = new StrictViewFragment ( ) ; fm . beginTransaction ( ) . replace ( R . id . fragmentContainer , fragment2 ) . addToBackStack ( null ) . commit ( ) ; FragmentTestUtil . executePendingTransactions ( mActivityRule ) ; FragmentTestUtil . assertChildren ( container , fragment2 ) ; assertEquals ( View . VISIBLE , fragment2 . getView ( ) . getVisibility ( ) ) ; fm . popBackStack ( ) ; FragmentTestUtil . executePendingTransactions ( mActivityRule ) ; Fragment replacement1 = fm . findFragmentByTag ( "1" ) ; assertNotNull ( replacement1 ) ; FragmentTestUtil . assertChildren ( container , replacement1 ) ; assertFalse ( replacement1 . isHidden ( ) ) ; assertTrue ( replacement1 . isAdded ( ) ) ; assertFalse ( replacement1 . isDetached ( ) ) ; assertEquals ( View . VISIBLE , replacement1 . getView ( ) . getVisibility ( ) ) ; }
Simple replace of one fragment in a container.
Simple replace of one fragment in a container .