View Binding proposals and Leak Canary integration
As a respond to !32 (comment 2903718), I had another look at Googles Android Developers Guides about view binding.
One problem we need to address is that the lifecycle of the Fragment outlives the views lifecycle, which leads to memory leaks.
For what I've read is there are 2 main recommended patterns for Fragments:
- inflating a fragment with the help of viewBinding. This requires to keep a nullable reference of the binding object and set it to null in onDestroyView, so that we avoid a possible memory leak.
-
inflating a fragment via it's constructor and bind in onViewCreated. In case we don't need a binding reference in the fragment, we can create a local reference in
OnViewCreated
that won't outlive the Fragment's view's lifecycle. This way we don't need to care at all about nulling binding objects.
In any case we should avoid initializing view bindings for fragments using lateinit
since that is a guarantee for a leak.
This MR proposes to change the use of view bindings so that one of the two patterns is always used, depending on whether we want to hold a reference to the binding object or not
It also adds LeakCanary to detect possible memory leaks early in the development cycle. LeakCanary is only added to debug builds, and only if they are not running as instrumentation tests.
(possible circular dependencies due to the implementation of the click handler in the fragment hasn't been yet addressed)
Using a local scoped view binder should also resolve circular dependencies related to the click handler.
@ankitgusai19 I'm curious to hear your opinion