, , . , :
@Provides
Context provideApplicationContext() {
return AlachiqApplication.getInstance();
}
@Provides
Context provideContext() {
return this.context;
}
, , Context .
. , .
, , - . Named. , , , . :
@Module
public class SpModules {
@Provides
@Named("context")
Context provideContext() {
return this.context;
}
}
@Module
public class RealmModule {
@Provides
@Named("application.context")
Context provideApplicationContext() {
return AlachiqApplication.getInstance();
}
}
, , , , :
public class Something {
public Something(@Named("application.context") Context context) {
}
}
:
public class Something {
@Named("application.context") Context context;
}
:
@Provides
@Singleton
SP provideSharePreferences(@Named("context") Context context) {
return new SP(context);
}
, .
Qualifiers. Named, . . , , 2 :
@java.lang.annotation.Documented
@java.lang.annotation.Retention(RUNTIME)
@javax.inject.Qualifier
public @interface InstanceContext {
}
@java.lang.annotation.Documented
@java.lang.annotation.Retention(RUNTIME)
@javax.inject.Qualifier
public @interface ApplicationContext {
}
:
@Module
public class SpModules {
@Provides
@InstanceContext
Context provideContext() {
return this.context;
}
}
@Module
public class RealmModule {
@Provides
@ApplicationContext
Context provideApplicationContext() {
return AlachiqApplication.getInstance();
}
}
, . :
public class Something {
public Something(@ApplicationContext Context context) {
}
}
public class Something {
@ApplicationContext Context context;
}
@Provides
@Singleton
SP provideSharePreferences(@InstanceContext Context context) {
return new SP(context);
}
, . , . , .