Reputation: 4030
Whats the meaning and purpose of @Inject
and / or @InjectView
annotations in Android / Java programming?
How can I use it?
Thanks in advance!
Upvotes: 16
Views: 12389
Reputation: 922
For Android, these annotations are part of the Roboguice framework. They are used to provide dependency injection in an Android environment.
This allows you to directly inject an instance of the desired resource, whether it's a basic POJO, a view, or another resource. Here's a POJO example from the RoboGuice wiki:
class MyActivity extends RoboActivity {
@Inject Foo foo; // this will basically call new Foo();
}
This is a trivial one, but essentially the injection point is allowing your class to stay independent of creating/managing an instance of the injected Foo class and instead puts that responsibility on Foo itself by calling Foo's default constructor in this case. This allows for easier testing via something like mocks since MyActivity is free from the details of actual creating Foo itself.
Upvotes: 16