fragment/src/androidTest/java/androidx/fragment/app/FragmentReorderingTest.java
@Test
    public void removeRedundantShow() throws Throwable {
        final CountCallsFragment fragment1 = new CountCallsFragment();
        int id = mFM.beginTransaction()
                .add(R.id.fragmentContainer, fragment1)
                .addToBackStack(null)
                .setReorderingAllowed(true)
                .commit();
        FragmentTestUtil.executePendingTransactions(mActivityRule);
        assertEquals(0, fragment1.onShowCount);
        assertEquals(0, fragment1.onHideCount);
        FragmentTestUtil.assertChildren(mContainer, fragment1);

        mInstrumentation.runOnMainSync(new Runnable() {
            @Override
            public void run() {
                mFM.beginTransaction()
                        .hide(fragment1)
                        .addToBackStack(null)
                        .setReorderingAllowed(true)
                        .commit();
                mFM.beginTransaction()
                        .detach(fragment1)
                        .addToBackStack(null)
                        .setReorderingAllowed(true)
                        .commit();
                mFM.beginTransaction()
                        .attach(fragment1)
                        .addToBackStack(null)
                        .setReorderingAllowed(true)
                        .commit();
                mFM.beginTransaction()
                        .show(fragment1)
                        .addToBackStack(null)
                        .setReorderingAllowed(true)
                        .commit();
                mFM.executePendingTransactions();
            }
        });

        FragmentTestUtil.assertChildren(mContainer, fragment1);
        // can optimize out the show/hide
        assertEquals(0, fragment1.onShowCount);
        assertEquals(0, fragment1.onHideCount);

        FragmentTestUtil.popBackStackImmediate(mActivityRule, id,
                FragmentManager.POP_BACK_STACK_INCLUSIVE);
        assertEquals(0, fragment1.onShowCount);
        assertEquals(0, fragment1.onHideCount);
    }
// hiding and showing the same view should optimize out
hiding and showing the same view should optimize out
hiding and showing the same view should optimize out
@Test
    public void removeRedundantShow() throws Throwable {
        final CountCallsFragment fragment1 = new CountCallsFragment();
        int id = mFM.beginTransaction()
                .add(R.id.fragmentContainer, fragment1)
                .addToBackStack(null)
                .setReorderingAllowed(true)
                .commit();
        FragmentTestUtil.executePendingTransactions(mActivityRule);
        assertEquals(0, fragment1.onShowCount);
        assertEquals(0, fragment1.onHideCount);
        FragmentTestUtil.assertChildren(mContainer, fragment1);
        mInstrumentation.runOnMainSync(new Runnable() {
            @Override
            public void run() {
                mFM.beginTransaction()
                        .hide(fragment1)
                        .addToBackStack(null)
                        .setReorderingAllowed(true)
                        .commit();
                mFM.beginTransaction()
                        .detach(fragment1)
                        .addToBackStack(null)
                        .setReorderingAllowed(true)
                        .commit();
                mFM.beginTransaction()
                        .attach(fragment1)
                        .addToBackStack(null)
                        .setReorderingAllowed(true)
                        .commit();
                mFM.beginTransaction()
                        .show(fragment1)
                        .addToBackStack(null)
                        .setReorderingAllowed(true)
                        .commit();
                mFM.executePendingTransactions();
            }
        });
        FragmentTestUtil.assertChildren(mContainer, fragment1);
        assertEquals(0, fragment1.onShowCount);
        assertEquals(0, fragment1.onHideCount);
        FragmentTestUtil.popBackStackImmediate(mActivityRule, id,
                FragmentManager.POP_BACK_STACK_INCLUSIVE);
        assertEquals(0, fragment1.onShowCount);
        assertEquals(0, fragment1.onHideCount);
    }
@ Test public void removeRedundantShow ( ) throws Throwable { final CountCallsFragment fragment1 = new CountCallsFragment ( ) ; int id = mFM . beginTransaction ( ) . add ( R . id . fragmentContainer , fragment1 ) . addToBackStack ( null ) . setReorderingAllowed ( true ) . commit ( ) ; FragmentTestUtil . executePendingTransactions ( mActivityRule ) ; assertEquals ( 0 , fragment1 . onShowCount ) ; assertEquals ( 0 , fragment1 . onHideCount ) ; FragmentTestUtil . assertChildren ( mContainer , fragment1 ) ; mInstrumentation . runOnMainSync ( new Runnable ( ) { @ Override public void run ( ) { mFM . beginTransaction ( ) . hide ( fragment1 ) . addToBackStack ( null ) . setReorderingAllowed ( true ) . commit ( ) ; mFM . beginTransaction ( ) . detach ( fragment1 ) . addToBackStack ( null ) . setReorderingAllowed ( true ) . commit ( ) ; mFM . beginTransaction ( ) . attach ( fragment1 ) . addToBackStack ( null ) . setReorderingAllowed ( true ) . commit ( ) ; mFM . beginTransaction ( ) . show ( fragment1 ) . addToBackStack ( null ) . setReorderingAllowed ( true ) . commit ( ) ; mFM . executePendingTransactions ( ) ; } } ) ; FragmentTestUtil . assertChildren ( mContainer , fragment1 ) ; assertEquals ( 0 , fragment1 . onShowCount ) ; assertEquals ( 0 , fragment1 . onHideCount ) ; FragmentTestUtil . popBackStackImmediate ( mActivityRule , id , FragmentManager . POP_BACK_STACK_INCLUSIVE ) ; assertEquals ( 0 , fragment1 . onShowCount ) ; assertEquals ( 0 , fragment1 . onHideCount ) ; }
hiding and showing the same view should optimize out
hiding and showing the same view should optimize out
// can optimize out the show/hide