I’m new to Hilt/Dagger. I have an existing project with a Singleton, and I need a parameter from the caller app stored in intent extras:
@Module
@InstallIn(SingletonComponent::class)
class MyModule
{
@Provides
@Singleton
fun provideMyClass(
@ApplicationContext appContext: Context,
// @ActivityContext activityContext: Context
): MyClass{
// Here I need a parameter I can normally retrieve from activity intent extras
// val myParam = (activityContext as CompatActivity).intent.getStringExtra('foo')
}
}
appContext
works correctly, but AFAIK I can’t retrieve the extras from the Application, I need the Activity
Other classes:
@HiltAndroidApp
class MyApplication : Application()
{
// ...
}
@AndroidEntryPoint
class MyActivity : ComponentActivity()
{
@Inject
lateinit var myClass: MyClass
//...
private fun myFunction() {
doStuff(myClass)
}
}
If I try to replace @ApplicationContext appContext: Context
with @ActivityContext activityContext: Context
, I get the error
error: [Dagger/MissingBinding] @dagger.hilt.android.qualifiers.ActivityContext android.content.Context cannot be provided without an @Provides-annotated method.
public abstract static class SingletonC implements HiltWrapper_AuthenticationServiceConnectionEntryPoint,
^
Note: A binding for @dagger.hilt.android.qualifiers.ActivityContext android.content.Context exists in mypackage.MyApplication_HiltComponents.ActivityC:
@dagger.hilt.android.qualifiers.ActivityContext android.content.Context is injected at
[mypackage.MyApplication_HiltComponents.SingletonC] mypackage.MyModule.provideMyClass(�, activityContext, �)
mypackage.MyClass is injected at
[mypackage.MyApplication_HiltComponents.ActivityC] mypackage.MyActivity.myClass
mypackage.MyActivity is injected at
[mypackage.MyApplication_HiltComponents.ActivityC] mypackage.MyActivity_GeneratedInjector.injectMyActivity(mypackage.MyActivity) [mypackage.MyApplication_HiltComponents.SingletonC ? mypackage.MyApplication_HiltComponents.ActivityRetainedC ? mypackage.MyApplication_HiltComponents.ActivityC]
The following other entry points also depend on it:
dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.ViewModelFactoriesEntryPoint.getHiltViewModelMap() [mypackage.MyApplication_HiltComponents.SingletonC ? mypackage.MyApplication_HiltComponents.ActivityRetainedC ? mypackage.MyApplication_HiltComponents.ViewModelC]
How can I get that parameter in there? The error seems to suggest I should put the @Provides
somewhere, but I don’t know where (it doesn’t seem to work on the Activity)