Dagger doesn't know about Android. Or anything, really. If you want to inject something, you have to tell Dagger about it.
You can see an example of how to inject a Context
in the examples. In this case, a qualifier is used to differentiate the application one from an activity one.
/**
* Allow the application context to be injected but require that it be annotated with
* {@link ForApplication @Annotation} to explicitly differentiate it from an activity context.
*/
@Provides @Singleton @ForApplication Context provideApplicationContext() {
return application;
}
Edit
No, you cannot inject an unqualified type and have the instance of that type change based on the context in which you are performing injection. Dagger requires that the source of a type is known at compile-time and since object graphs are immutable that source cannot be changed.
The only way to do this is to use a factory which allows you to specify the context with which the object will be created.
public final class ThingFactory {
private final Foo foo;
private final Bar bar;
@Inject public ThingFactory(Foo foo, Bar bar) {
this.foo = foo;
this.bar = bar;
}
public Thing get(Context context) {
return new Thing(context, foo, bar);
}
}