question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AutoClearedValue cleans reference on Destroy of other fragment

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
thaihuynhxyzcommented, Jul 10, 2017

@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;
    }
}
1reaction
yigitcommented, Jul 10, 2017

you are right, sorry about that.

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

AutoClearedValue cleans reference on Destroy of ... - GitHub
For Example - From the Base Fragment, open a DialogFragment ... AutoClearedValue cleans reference on Destroy of other fragment #56.
Read more >
Let your delegates auto-nullify references☠️ | by Shreyas Patil
Fragments outlive their views. Make sure you clean up any references to the binding class instance in the fragment's onDestroyView() method. In ...
Read more >
Let your delegates auto-nullify references☠️ - Shreyas Patil
Fragments outlive their views. Make sure you clean up any references to the binding class instance in the fragment's onDestroyView() method.
Read more >
AutoClearedValue accessed from another thread after View is ...
I am using AutoClearedValue class from this link and when view is destroyed, backing field becomes null and that is good but i...
Read more >
architecture-components-samples/AutoClearedValue.kt at main
* A lazy property that gets cleaned up when the fragment's view is destroyed. *. * Accessing this variable while the fragment's view...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found