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

        // the add detach is not fully optimized out
        assertEquals(1, fragment1.onAttachCount);
        assertEquals(0, fragment1.onDetachCount);
        assertTrue(fragment1.isDetached());
        assertEquals(0, fragment1.onCreateViewCount);
        FragmentTestUtil.assertChildren(mContainer);

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

        FragmentTestUtil.assertChildren(mContainer);
        // can optimize out the attach/detach, and the hide call
        assertEquals(1, fragment1.onAttachCount);
        assertEquals(0, fragment1.onDetachCount);
        assertEquals(1, fragment1.onHideCount);
        assertTrue(fragment1.isHidden());
        assertEquals(0, fragment1.onShowCount);

        mFM.popBackStack(id, 0);
        FragmentTestUtil.executePendingTransactions(mActivityRule);
        FragmentTestUtil.assertChildren(mContainer);

        // we can optimize out the attach/detach on the way back
        assertEquals(1, fragment1.onAttachCount);
        assertEquals(0, fragment1.onDetachCount);
        assertEquals(1, fragment1.onShowCount);
        assertEquals(1, fragment1.onHideCount);
        assertFalse(fragment1.isHidden());
    }
// attaching, then detaching shouldn't result in a View being created
attaching, then detaching shouldn't result in a View being created
attaching then detaching shouldn ' t result in a View being created
@Test
    public void removeRedundantDetach() throws Throwable {
        final CountCallsFragment fragment1 = new CountCallsFragment();
        int id = mFM.beginTransaction()
                .add(R.id.fragmentContainer, fragment1)
                .detach(fragment1)
                .addToBackStack(null)
                .setReorderingAllowed(true)
                .commit();
        FragmentTestUtil.executePendingTransactions(mActivityRule);
        assertEquals(1, fragment1.onAttachCount);
        assertEquals(0, fragment1.onDetachCount);
        assertTrue(fragment1.isDetached());
        assertEquals(0, fragment1.onCreateViewCount);
        FragmentTestUtil.assertChildren(mContainer);
        mInstrumentation.runOnMainSync(new Runnable() {
            @Override
            public void run() {
                mFM.beginTransaction()
                        .attach(fragment1)
                        .addToBackStack(null)
                        .setReorderingAllowed(true)
                        .commit();
                mFM.beginTransaction()
                        .hide(fragment1)
                        .addToBackStack(null)
                        .setReorderingAllowed(true)
                        .commit();
                mFM.beginTransaction()
                        .detach(fragment1)
                        .addToBackStack(null)
                        .setReorderingAllowed(true)
                        .commit();
                mFM.executePendingTransactions();
            }
        });
        FragmentTestUtil.assertChildren(mContainer);
        assertEquals(1, fragment1.onAttachCount);
        assertEquals(0, fragment1.onDetachCount);
        assertEquals(1, fragment1.onHideCount);
        assertTrue(fragment1.isHidden());
        assertEquals(0, fragment1.onShowCount);
        mFM.popBackStack(id, 0);
        FragmentTestUtil.executePendingTransactions(mActivityRule);
        FragmentTestUtil.assertChildren(mContainer);
        assertEquals(1, fragment1.onAttachCount);
        assertEquals(0, fragment1.onDetachCount);
        assertEquals(1, fragment1.onShowCount);
        assertEquals(1, fragment1.onHideCount);
        assertFalse(fragment1.isHidden());
    }
@ Test public void removeRedundantDetach ( ) throws Throwable { final CountCallsFragment fragment1 = new CountCallsFragment ( ) ; int id = mFM . beginTransaction ( ) . add ( R . id . fragmentContainer , fragment1 ) . detach ( fragment1 ) . addToBackStack ( null ) . setReorderingAllowed ( true ) . commit ( ) ; FragmentTestUtil . executePendingTransactions ( mActivityRule ) ; assertEquals ( 1 , fragment1 . onAttachCount ) ; assertEquals ( 0 , fragment1 . onDetachCount ) ; assertTrue ( fragment1 . isDetached ( ) ) ; assertEquals ( 0 , fragment1 . onCreateViewCount ) ; FragmentTestUtil . assertChildren ( mContainer ) ; mInstrumentation . runOnMainSync ( new Runnable ( ) { @ Override public void run ( ) { mFM . beginTransaction ( ) . attach ( fragment1 ) . addToBackStack ( null ) . setReorderingAllowed ( true ) . commit ( ) ; mFM . beginTransaction ( ) . hide ( fragment1 ) . addToBackStack ( null ) . setReorderingAllowed ( true ) . commit ( ) ; mFM . beginTransaction ( ) . detach ( fragment1 ) . addToBackStack ( null ) . setReorderingAllowed ( true ) . commit ( ) ; mFM . executePendingTransactions ( ) ; } } ) ; FragmentTestUtil . assertChildren ( mContainer ) ; assertEquals ( 1 , fragment1 . onAttachCount ) ; assertEquals ( 0 , fragment1 . onDetachCount ) ; assertEquals ( 1 , fragment1 . onHideCount ) ; assertTrue ( fragment1 . isHidden ( ) ) ; assertEquals ( 0 , fragment1 . onShowCount ) ; mFM . popBackStack ( id , 0 ) ; FragmentTestUtil . executePendingTransactions ( mActivityRule ) ; FragmentTestUtil . assertChildren ( mContainer ) ; assertEquals ( 1 , fragment1 . onAttachCount ) ; assertEquals ( 0 , fragment1 . onDetachCount ) ; assertEquals ( 1 , fragment1 . onShowCount ) ; assertEquals ( 1 , fragment1 . onHideCount ) ; assertFalse ( fragment1 . isHidden ( ) ) ; }
attaching, then detaching shouldn't result in a View being created
attaching then detaching shouldn ' t result in a View being created
// the add detach is not fully optimized out // can optimize out the attach/detach, and the hide call // we can optimize out the attach/detach on the way back