1

I read some articles where it is said that the best practice is to set databinding obejct to null inside onDestroyView() in a fragment:

override fun onDestroyView() {
    super.onDestroyView()
    dataBinding = null
}

I've also seen this video where this practice is not even mentioned. I personally don't understand if this practice makes sense or not. Please provide some info about this issue because I really don't know how to go further. Thanks

Lis Dya
  • 317
  • 3
  • 14

3 Answers3

0

Not mandatory but if you don't it could lead to memory leaks.

Fragment's lifecycle is different from Fragment's view lifecycle.

Consider the following example:

onCreate()
onCreateView()
onDestroyView()
onCreateView()
onDestroyView()
onDestroy()

In this case the user did an action which sent the fragment into the backstack then he pressed the back button. onCreateView has been called twice therefore two instances of dataBinding of which the first one has not been garbage collected. Of course the difference won't be noticeable in this case but if you have a big backstack you need to consider releasing all of your View references inside onDestroyView.

Michaël Randria
  • 453
  • 1
  • 5
  • 21
-1

This is not necessary. databinding is just auto generated by compile system. You can go to see the generated source code. Just use databinding object as same as common propertys.

leimenghao
  • 226
  • 1
  • 10
  • I'll wait also for other answers and if I don't get a better one, I'll mark your as accepted. Thank you. – Lis Dya Jul 02 '20 at 11:41
-1

Not necessary. Needed for DB close, Network call, Services etc.

Mr_vmh
  • 171
  • 1
  • 9