What's the best practice for calling : -
Butterknife.unbind()
in a custom Android view please?
onDetachedFromWindow
won't always work, like if the custom view is within a RecyclerView
. Adding that in fact actually crashed my app. Honestly, it worked fine without unbinding it.
Yes, onDetachedFromWindow
is the right function as mentioned in NJ's answer because this is where view no longer has a surface for drawing.
But the usage is incorrectly mentioned in the answer. The right approach involves binding in onFinishInflate()
:
@Override
protected void onFinishInflate() {
super.onFinishInflate();
unbinder = ButterKnife.bind(this);
}
and unbinding in onDetachedFromWindow
:
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
// View is now detached, and about to be destroyed
unbinder.unbind();
}
Try in onDetachedFromWindow()
Unbinder unbinder;
unbinder = Butterknife.bind(this, root);
and in onDetachedFromWindow
you need to call unbinder.unbind();
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
// View is now detached, and about to be destroyed
unbinder.unbind()
}
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