0

I'm new to Android. I wrote a custom view, but I just don't know where to call the bind and unbind method. I have searched for this documentation. And it seemingly suggests to use bind in the onFinishInflate() callback. But I bind the view in its constructor function and there is no crash any way. Is it correct? And how about the unbind? I find this question, it suggests to use unbind in the onDetachedFromWindow() callback. Is it required or necessary?

public class BloodIndicatorView extends FrameLayout {
    @Bind(R.id.ll_record_bloodpress)
    LinearLayout llRecordBloodpress;
    @Bind(R.id.ll_record_bloodsugar)
    LinearLayout llRecordBloodsugar;

    private Context mContext;

    public BloodIndicatorView(Context context) {
        this(context, null);
    }

    public BloodIndicatorView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public BloodIndicatorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    private void init() {
        LayoutInflater.from(mContext).inflate(R.layout.health_blood_indicator, this);
        ButterKnife.bind(this);
    }

    public void update() {

    }

    @OnClick(R.id.ll_record_bloodpress)
    public void recordBloodpress() {
        Intent intent = BloodPressActivity.getIntent2Act(mContext);
        mContext.startActivity(intent);
    }

    @OnClick(R.id.ll_record_bloodsugar)
    public void recordBloodsugar() {
        Intent intent = BloodSugarActivity.getIntent2Act(mContext);
        mContext.startActivity(intent);
    }
}
Community
  • 1
  • 1
DysaniazzZ
  • 825
  • 15
  • 29

1 Answers1

0

You can annotate the view inside the class which you are doing correctly and since there is no error that means ButterKnife.bind(this); is happening correctly. And its not necessary to unbind this should work completely fine.

Gaurav Sarma
  • 2,248
  • 2
  • 24
  • 45