fragment/src/androidTest/java/androidx/fragment/app/LoaderTest.java
@Test
    public void testLeak() throws Throwable {
        // Restart the activity because mActivityRule keeps a strong reference to the
        // old activity.
        LoaderActivity activity = FragmentActivityUtils.recreateActivity(mActivityRule,
                mActivityRule.getActivity());

        LoaderFragment fragment = new LoaderFragment();
        FragmentManager fm = activity.getSupportFragmentManager();

        fm.beginTransaction()
                .add(fragment, "1")
                .commit();

        FragmentTestUtil.executePendingTransactions(mActivityRule, fm);

        fm.beginTransaction()
                .remove(fragment)
                .addToBackStack(null)
                .commit();

        FragmentTestUtil.executePendingTransactions(mActivityRule, fm);
        fm = null; // clear it so that it can be released

        WeakReference<RecreatedActivity> weakActivity =
                new WeakReference<>(LoaderActivity.sActivity);

        activity = FragmentActivityUtils.recreateActivity(mActivityRule, activity);

        // Wait for everything to settle. We have to make sure that the old Activity
        // is ready to be collected.
        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
        FragmentTestUtil.waitForExecution(mActivityRule);

        // Force a garbage collection.
        FragmentTestUtil.forceGC();
        assertNull(weakActivity.get());
    }
/**
     * Test to ensure that there is no Activity leak due to Loader
     */
Test to ensure that there is no Activity leak due to Loader
Test to ensure that there is no Activity leak due to Loader
@Test
    public void testLeak() throws Throwable {
        LoaderActivity activity = FragmentActivityUtils.recreateActivity(mActivityRule,
                mActivityRule.getActivity());
        LoaderFragment fragment = new LoaderFragment();
        FragmentManager fm = activity.getSupportFragmentManager();
        fm.beginTransaction()
                .add(fragment, "1")
                .commit();
        FragmentTestUtil.executePendingTransactions(mActivityRule, fm);
        fm.beginTransaction()
                .remove(fragment)
                .addToBackStack(null)
                .commit();
        FragmentTestUtil.executePendingTransactions(mActivityRule, fm);
        fm = null; 
        WeakReference<RecreatedActivity> weakActivity =
                new WeakReference<>(LoaderActivity.sActivity);
        activity = FragmentActivityUtils.recreateActivity(mActivityRule, activity);
        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
        FragmentTestUtil.waitForExecution(mActivityRule);
        FragmentTestUtil.forceGC();
        assertNull(weakActivity.get());
    }
@ Test public void testLeak ( ) throws Throwable { LoaderActivity activity = FragmentActivityUtils . recreateActivity ( mActivityRule , mActivityRule . getActivity ( ) ) ; LoaderFragment fragment = new LoaderFragment ( ) ; FragmentManager fm = activity . getSupportFragmentManager ( ) ; fm . beginTransaction ( ) . add ( fragment , "1" ) . commit ( ) ; FragmentTestUtil . executePendingTransactions ( mActivityRule , fm ) ; fm . beginTransaction ( ) . remove ( fragment ) . addToBackStack ( null ) . commit ( ) ; FragmentTestUtil . executePendingTransactions ( mActivityRule , fm ) ; fm = null ; WeakReference < RecreatedActivity > weakActivity = new WeakReference < > ( LoaderActivity . sActivity ) ; activity = FragmentActivityUtils . recreateActivity ( mActivityRule , activity ) ; InstrumentationRegistry . getInstrumentation ( ) . waitForIdleSync ( ) ; FragmentTestUtil . waitForExecution ( mActivityRule ) ; FragmentTestUtil . forceGC ( ) ; assertNull ( weakActivity . get ( ) ) ; }
Test to ensure that there is no Activity leak due to Loader
Test to ensure that there is no Activity leak due to Loader
// Restart the activity because mActivityRule keeps a strong reference to the // old activity. // clear it so that it can be released // Wait for everything to settle. We have to make sure that the old Activity // is ready to be collected. // Force a garbage collection.