Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event observers in fragments are leaked when fragment is removed from activity #161

Open
goodsoft opened this issue Apr 15, 2015 · 8 comments
Milestone

Comments

@goodsoft
Copy link
Contributor

As event observers are registered in onResume and unregistered in onPause callbacks, if fragment is removed from activity without activity pausing, event observers are not unregistered and are left hanging, leaking fragment and causing unwanted effects when the event is triggered.
Maybe we should additionally unregister event handlers in onDestroy?

@johncarl81
Copy link
Owner

Are you removing the Fragment via getFragmentManager().beginTransaction().remove(fragment).commit() ?

@goodsoft
Copy link
Contributor Author

Mostly by .replace(R.id.container, newFragment), but it's the same thing, I think.

@johncarl81
Copy link
Owner

Hmm, the Fragment should be going through the "reverse series of callbacks" when it is "no longer used": http://developer.android.com/reference/android/app/Fragment.html#Lifecycle which includes onPause()

I'll have to investigate this a bit more to see what and if we should change. We should register and unregister in one specific, guaranteed callback respectively.

BTW, looking at Otto's example, it seems they also unregister in the onPause() method as well:
https://github.com/square/otto/tree/master/otto-sample/src/main/java/com/squareup/otto/sample

@goodsoft
Copy link
Contributor Author

I have looked at this once again and the problem apparently was with the fact that I was removing parent fragment without removing fragments embedded into it. So onPause was called for parent fragment, but wasn't called for child fragments.
Anyway, the problem is outside of Transfuse scope, so closing the issue.
Sorry for disturbance.

@goodsoft
Copy link
Contributor Author

Okay, the issue was that I was using injected FragmentManager in the parent fragment, which is injected via call to getFragmentManager().
If I instead use getChildFragmentManager() for adding/removing child fragments, they are automatically removed with parent fragment and the issue is solved.
Maybe add some way of injecting getChildFragmentManager into fragments?

@goodsoft goodsoft reopened this Apr 15, 2015
@johncarl81
Copy link
Owner

Good deal. I do appreciate the post even if it is a false alarm. Keep the input coming!

@johncarl81
Copy link
Owner

Absolutely. We would just need to add it to the FragmentPlugin. How would you like to inject it? We could specify a qualifier annotation for this injection since htere are many different types of FragmentManagers:
@ChildFragmentManager FragmentManager manager;

@goodsoft
Copy link
Contributor Author

As you see it fit :)

@johncarl81 johncarl81 added this to the 0.3.0 Release milestone Aug 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants