Why is it important to clear the binding in onDestroyView when implementing Android View Binding in a Fragment?
As the documentation shows, Use view binding in fragments, and the Android Architecture Components sample, the binding is initiated in onCreateView, and cleared in onDestroyView.
What is the benefit of implementing this compared to initiating the binding without clearing it in onDestroyView? Without explicitly clearing the instance variable binding, it should be cleared when the Fragment is destroyed.
private var _binding: ResultProfileBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = ResultProfileBinding.inflate(inflater, container, false)
val view = binding.root
return view
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}