https://kotlinlang.org logo
a

Archie

05/18/2020, 7:14 AM
Hi guy, I would life to ask about Android's
ViewBinding
. In the documentation here, it suggest to clear the binding reference, in on
onDestroyView()
:
Copy code
override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
}
because: "Note: Fragments outlive their views. Make sure you clean up any references to the binding class instance in the fragment's 
onDestroyView()
 method." My questions are: 1. When exactly would the fragment outlive the views? Is it when the fragment is added to the backstack? 2. Does avoiding a global reference to the
_binding
and simply referencing it locally in
onViewCreated()
ensures that I wont leak the
viewBinding
? ex.
Copy code
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    val binding = SomeViewBinding.inflate(view)
}