Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly happens when you fail to use unbind()?

I have an app where I use Butterknife, and recently I found a fragment where I had failed to call unbinder.unbind() in the fragment's onDestroyView(). I fixed the problem but it made me start thinking.

What kind of errors can this cause and why? I don't have a particular error right now but I would like to know what to watch out for in the future, and the website for the library doesn't specify the problems this could cause.

like image 605
drawinfinity Avatar asked Oct 30 '22 04:10

drawinfinity


1 Answers

Imagine you have a retained fragment and you have initialized a view using @BindView.

An orientation change happens, which results in destroying activity instance, but not this fragment, because this fragment is a retained fragment, which means, that the field that you have initialized is still there (not null) and is holding a strong reference to the view of the previous activity, which results in leaking of the activity.

Although this might take for some small amount of time (because eventually you are going to perform another ButterKnife.bind() in onViewCreated(), right? But who knows, maybe you won't), still it is better to release resources as soon as you do not need them and let the GC do its job.

I've also thought about this question some time ago and other than this I couldn't come up to another scenario where unbind() would be strongly necessary.

like image 112
azizbekian Avatar answered Nov 10 '22 15:11

azizbekian