Custom look Butterknife unbind

What is best for calls: -

Butterknife.unbind() 

in android user view please?

+7
android android-view butterknife
source share
3 answers

Yes, onDetachedFromWindow is the right function, as stated in the NJ answer , because here the view no longer has a drawing surface.

But use is incorrectly indicated in the answer. The correct approach involves binding in onFinishInflate() :

 @Override protected void onFinishInflate() { super.onFinishInflate(); unbinder = ButterKnife.bind(this); } 

and untie in onDetachedFromWindow :

 @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); // View is now detached, and about to be destroyed unbinder.unbind(); } 
+17
source share

Try 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() } 
+8
source share

Attention!

If you set attributes with app:attribute="value" in XML, you lose their values ​​when reading with:

 @Override protected void onFinishInflate() { super.onFinishInflate(); unbinder = ButterKnife.bind(this); TypedValue typedValue = new TypedValue(); TypedArray typedArray = getContext().obtainStyledAttributes(typedValue.data, R.styleable.YourStyleable); try { int number = typedArray.getResourceId(R.styleable.YourStyleable_number, 0); image.setImageResource(number); String text = typedArray.getString(R.styleable.YourStyleable_text); text.setText(text); } finally { typedArray.recycle(); } } 

Their values ​​will be 0 and null. Initialize them in the custom view designer.

The reason is to use obtainStyledAttributes(typedValue.data instead of obtainStyledAttributes(attrs .

See: Magic with the getStyledAttributes method .

0
source share

All Articles