I stumbled across a very interesting Dependency Injection library called ButterKnife
. Using ButterKnife
it's easily possible to inject Views into activities or fragments.
class ExampleActivity extends Activity {
@InjectView(R.id.title) TextView title;
@InjectView(R.id.subtitle) TextView subtitle;
@InjectView(R.id.footer) TextView footer;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
ButterKnife.inject(this);
// TODO Use "injected" views...
}
}
However if using Dependency Injection those Views must be public
so that Butterknife
can inject it (using private
fields results in an exception fields must not be private or static
).
In my past project I always made all the member fields (including the views) private
as I thought this is best practice (information hiding etc.) Now I am wondering if there is a reason why one should not make all the views public
? In this case I cannot use ButterKnife
but I want to use it because it simplifies the code a lot.
First off, Butter Knife is not a dependency injection library. You can think of it as a boilerplate reduction library since all it does is replace
findViewById
and varioussetXxxListener
calls.The reason that Butter Knife requires views not be private is that is actually generates code which sets the fields. The code that it generates lives in the same package as your class which is why the field must be package-private, protected, or public. If the field was private the generated code would fail to compile since it cannot access the private field.
The generated code looks something like this:
When you call
ButterKnife.inject(this)
it looks up this generate class and calls theinject
method with your instance ofExampleActivity
as both the destination for the fields and the source forfindViewById
calls.