Architecture-components-samples: AutoClearedValue cleans reference on Destroy of other fragment

Created on 11 Jun 2017  路  5Comments  路  Source: android/architecture-components-samples

onFragmentViewDestroyed is being called even if the Fragment that is being destroyed is not the Holding Fragment. For Example - From the Base Fragment, open a DialogFragment

DialogFragment fragment = new SomeFragment();
fragment.show(getFragmentManager(), "tag");

On coming back from the Dialog Fragment, All AutoClearedView instances will get cleared in the Base Fragment. add same instance check on the Fragment solves the issue

bug

All 5 comments

That looks like a bug in the fragments library, we only observe the given fragment, not sub fragments. Can you please report a bug in issuetracker.google.com ?
Meanwhile, I'll fix this inside the project.

I added a test for this, does not seem to be broken.
https://github.com/googlesamples/android-architecture-components/pull/79/files

@yigit
Because of your test missing dismiss before assert testFragment.testValue.get() with "foo"
Test should be:

   @Test
    public void testDontClearForDialog() throws Throwable {
        testFragment.testValue = new AutoClearedValue<>(testFragment, "foo");
        DialogFragment dialogFragment = new DialogFragment();
        dialogFragment.show(testFragment.getFragmentManager(), "dialog");
        dialogFragment.dismiss();
        InstrumentationRegistry.getInstrumentation().waitForIdleSync();
        assertThat(testFragment.testValue.get(), is("foo"));
    }

Can we fix this bug with check the fragment that is destroyed view is the fragment registerFragmentLifecycleCallbacks?

    public AutoClearedValue(Fragment fragment, T value) {
        FragmentManager fragmentManager = fragment.getFragmentManager();
        fragmentManager.registerFragmentLifecycleCallbacks(
                new FragmentManager.FragmentLifecycleCallbacks() {
                    @Override
                    public void onFragmentViewDestroyed(FragmentManager fm, Fragment f) {
                        if(fragment == f) {
                            AutoClearedValue.this.value = null;
                            fragmentManager.unregisterFragmentLifecycleCallbacks(this);
                        }
                    }
                },false);
        this.value = value;
    }
}

you are right, sorry about that.

I've updated the PR and also reported here: https://issuetracker.google.com/issues/63514149

ok looks like it was a bad assumption on my side. since we are observing the fragment manager (not the fragment itself), we need to check the fragment instance.
thanks for the report.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nioncode picture nioncode  路  4Comments

george5613 picture george5613  路  4Comments

prabinshrestha picture prabinshrestha  路  5Comments

khalid64927 picture khalid64927  路  3Comments

prabinshrestha picture prabinshrestha  路  3Comments