Is it necessary to set ViewBinding to null in Fragment's onDestroy()? Sometimes I see that thing in tutorials. Is it really necessary?
It is necessary and a really good practice, specially in Android where memory restrictions are huge, you really need to take care of cleaning up resources as and when you are done with them. ViewBinding
will generate a custom ViewBinding
class which will keep references to all your views
inside Fragment
, if ViewBinding is not cleared or set to null, it won't be eligible for GC
, thereby holding all the views in memory even though you are not using it, leading to memory leaks. So yes, it is always better to set it to null
at the end of life cycle.
Yes it is recommended to set ViewBinding to null in onDestroyView
.
ViewBinding is scoped to the lifecycle of the fragment's view (between onCreateView
and onDestroyView
). i.e. it is is only valid between onCreateView and onDestroyView.
Hence, please set the respective ViewBinding to null in fragment's onDestroyView
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With