Thomas
06/11/2020, 4:54 PM@InstallIn(FragmentComponent::class)
@Module
object MyFragmentModule {
@Provides
@FragmentScope
internal fun providesViewModel(
fragment: MyFragment,
anotherDependency: Dependency
) = MyViewModel(
fragment.requireArguments().getString("argument"),
anotherDependency
)
}
@AndroidEntryPoint
class MyFragment : Fragment() {
// ...
}
However, this does not appear to work as it shows the following error when building:
error: [Dagger/MissingBinding] com.myapp.MyFragment cannot be provided without an @Inject constructor or an @Provides-annotated method. This type supports members injection but cannot be implicitly provided.
Am I doing something wrong? This worked fine without Hilt when using Dagger Android.Ahmed Ibrahim
06/11/2020, 4:57 PMMyFragment
with a mere Fragment
?Ahmed Ibrahim
06/11/2020, 4:58 PMFragment
should work.Thomas
06/11/2020, 5:00 PMAhmed Ibrahim
06/11/2020, 5:01 PM@BindsInstance
for your custom Fragment type.
https://dagger.dev/hilt/custom-componentsThomas
06/11/2020, 5:02 PMAhmed Ibrahim
06/11/2020, 5:03 PMFragment
to your MyFragment
Ahmed Ibrahim
06/11/2020, 5:03 PM@Binds
fun bindsMyFragment(f: Fragment): MyFragment
Ahmed Ibrahim
06/11/2020, 5:04 PMThomas
06/11/2020, 5:04 PMThomas
06/11/2020, 6:03 PM@Provides
@FragmentScope
internal fun providesViewModel(
fragment: Fragment,
anotherDependency: Dependency
) = MyViewModel(
(fragment as MyFragment).myArgument,
anotherDependency
)
Ahmed Ibrahim
06/11/2020, 6:05 PMJavier
06/12/2020, 7:52 AM@ViewModelInject
?Thomas
06/12/2020, 7:57 AM@ViewModelInject
as I need to pass Fragment args to the view model.