Dagger 2, Providing Application Context In Module
Solution 1:
This is how It is done. Using @BindsInstance in your component will inject application to all of your modules.In your case just to AppModule
@Singleton@Component(modules = arrayOf(AppModule::class))
interfaceAppComponent{
@Component.Builder
interfaceBuilder() {
funbuild(): AppComponent
@BindsInstancefunapplication(application: Application): Builder
}
}
** Delete code to "Provides application" function in your APP module and make sure you pass application context to create sharedPreferences.
@Module
class AppModule {
@Provides@Singleton
fun provideSharedPrefManager(context: Application): SharedPreferencesManager =
SharedPreferencesManager(context)
}
and now in your onCreate of applicationClass
DaggerAppComponent.builder().application(this).build()
Optional: if you want to inject something into your applicationClass then do the following
DaggerAppComponent.builder().application(this).build().inject(this)
Solution 2:
You can't use class PineApplication @Inject constructor(): Application()
to create PineApplication
. It's a framework class and has to be created by the Android Framework.
Doing so Dagger will create PineApplication
, but applicationContext
will return null
as it has never been initialized (by the system).
Don't use constructor injection for framework classes and don't create the yourself. Use @Bindsintance
to add the object to the component with its builder, or use a module to provide it.
Solution 3:
For provide application context, you can create class, like ComponentsManager
with:
publicclassComponentsManager {
private Context context;
private AppComponent appComponent;
publicComponentsManager(Context context) {
this.context = context.getApplicationContext();
}
public AppComponent getAppComponent(){
if (appComponent == null){
appComponent = DaggerAppComponent.builder()
.appModule(new AppModule(context))
.build();
}
return appComponent;
}
}
And in your application class init this ComponentsManager
like this:
publicclassYourAppextendsApplication {
privatestaticComponentsManager componentsManager;
@OverridepublicvoidonCreate() {
super.onCreate();
initComponentsManager();
initAppComponent();
}
publicstaticComponentsManagergetComponentsManager(){
return componentsManager;
}
privatevoidinitComponentsManager(){
componentsManager = newComponentsManager(this);
}
privatevoidinitAppComponent(){
componentsManager.getAppComponent();
}
}
Solution 4:
You can try modifying your App module like this.
@ModuleclassApplicationModule(privateval application: Application) {
@Provides@SingletonfunprovideContext(): Context {
return application.applicationContext
}
@Provides@SingletonfunprovideSharedPreferences(context: Context): SharedPreferences {
return context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE)
}
}
Then you can build the dagger component like this from your Application class.
val appComponent = DaggerAppComponent.builder()
.applicationModule(ApplicationModule(this))
.build()
Inject values in presenter like this.
application.appComponent.inject(this)
Post a Comment for "Dagger 2, Providing Application Context In Module"