Open almeric opened 3 years ago
Facing the same problem as well with Hilt's FragmentContextWrapper, however I'm not sure if that's Epoxy's fault or Hilt's.
Maybe If Hilt somehow can provide a way to disable the usage of that wrapper since not all views needs to be injected, that would solve the problem.
Clearing the adapter in onDestroyView
seems to fix the problem without the need to use the Activity's LayoutInflater.
override fun onDestroyView() {
requireView().findViewById<RecyclerView>(R.id.epoxy_view).adapter = null
super.onDestroyView()
}
From https://github.com/google/dagger/issues/2070
Our initial assumption was that a view created by a fragment shouldn't outlive the fragment, so there should never be a leak. Keeping with that assumption, you can avoid the issue by explicitly using the activity context when inflating a view that is meant to outlive its parent fragment.
The point of EpoxyRecyclerView's shared view pool is to reuse views across all fragments in an Activity as an optimization. Hilt's design breaks this, as they are make a bad assumption (which they recognize is an issue).
Besides the work around of inflating your views with the activity inflater, you can also disable Epoxy's context wide view recycling - EpoxyRecyclerView#shouldShareViewPoolAcrossContext
If you care about this it may also be helpful for you to weigh in on the Hilt issue (https://github.com/google/dagger/issues/2070) and encourage them to fix it on their side.
@elihart can you please tell me how to disable Epoxy's context wide view recycling - EpoxyRecyclerView#shouldShareViewPoolAcrossContext???
I've just migrated one of our apps to Dagger Hilt and now LeakCanary 2 is finding leaks in our fragments that use Epoxy.
The issue seems very similar to #886, but instead of a
ContextWrapper
Hilt gives us anFragmentContextWrapper
instead. See here: https://github.com/google/dagger/blob/71509b841b30909ddf6cb36ed366d4a609d84071/java/dagger/hilt/android/internal/managers/ViewComponentManager.java#L169LeakCanary trace
This happens in both the latest 4.0 beta & latest stable version of Epoxy
One way I've found to fix it is using a different context when inflating the view.
Instead of using the supplied LayoutInflater, fetching it from the activity seems to solve the issue: