What exactly happens when you do not use unbind ()?

I have an application in which I use Butterknife, and recently I found a fragment in which I was not able to call unbinder.unbind() in the onDestroyView() fragment. I fixed the problem, but it made me think.

What errors can be the reason and why? I don’t have a specific error right now, but I would like to know what should be monitored in the future, and the library website does not indicate any problems that may arise.

+5
source share
1 answer

Imagine you have a saved snippet and you initialized the view using @BindView .

a change in orientation occurs, which leads to the destruction of the activity instance, but not to this fragment, since this fragment is a saved fragment, which means that the initialized field still exists (not null ) and a strong reference to the opinion of the previous activity is retained, which leads to activity leak.

Although this may take some time (because in the end you are going to execute another ButterKnife.bind() in onViewCreated() , right? But who knows, maybe you won’t), it's better to free up resources as soon as you don’t need them, and let the GC do its job.

I also thought about this question a while ago, and besides this I could not come to another scenario where unbind() would be absolutely necessary.

+2
source

Source: https://habr.com/ru/post/1270226/


All Articles