Search code examples
androidbutterknife

Butterknife - multiple injections


I have an Activity as a target for Butterknife and I want to use the same Activity as a target for another View I am inflating in runtime. Is there a way to do it?

This is what I've tried and it doesn't work:

@InjectView(R.id.main)
TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    ButterKnife.inject(this);
    createDialog();
}

void createDialog() {
    View v = View.inflate(...);  // v has a view inside with id R.id.tv
    ButterKnife.inject(this, v);
    new Dialog(this).setView(v)....show();
}

@OnClick(R.id.tv)
void click() {
    // ...
}

Solution

  • You can't inject into the same object twice. Use two different objects. One can be the activity but one needs to be something else. It can be anything, just a simple object which holds all the fields for the dialog, for example.

    There's simply no way to inject into the same object twice.

    Issue comment by Jake Wharton