4. Magic Type infer
@Override
public <T extends View> T findViewById(@IdRes int id) {
return getDelegate().findViewById(id);
}
@Nullable
public final <T extends View> T findViewById(@IdRes int id) {
if (id == NO_ID) {
return null;
}
return findViewTraversal(id);
}
8. How to use
class ExampleActivity extends Activity {
@BindView(R.id.title) TextView title;
@BindView(R.id.subtitle) TextView subtitle;
@BindView(R.id.footer) TextView footer;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
ButterKnife.bind(this); // return an unbinder
// TODO Use fields...
}
}
9. Unbinder
• Return by `ButterKnife.bind()`
• Will release view reference to null
• Avoid in memory leak (mostly in Fragment case)
10. Pros & Cons
• Easy integrate
• Clean structure
• Annotation processing is slow
• One more dependency
• Upgrade
• Replace(Kotlin / databinding)