0

There is a generic fragment

public class MyFragment<TYPE> extends Fragment {

@Inject 
SomeClass class;

public MyFragment(){}

}
}

I am not able to add this Fragment to the Fragment Binding Module as Dagger complains it is of raw type.

How do I mention the TYPE class in the binding module?

My binding module looks like this for now:

@Module
public abstract class BindingModule {

    @ContributesAndroidInjector(modules = MyFragmentModule.class)
    abstract MyFragment bindMyFragment();


} 

Error

error: [dagger.android.AndroidInjector.inject(T)] MyFragment has type parameters, cannot members inject the raw type.
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Sunny
  • 7,444
  • 22
  • 63
  • 104
  • I haven't use it yet with ```AndroidInjector```, but you could try to use diamond ```<>``` with ```super``` or ```extend``` keywords for upper or lower bounded wildcards https://docs.oracle.com/javase/tutorial/java/generics/wildcardGuidelines.html – Joe Dow Dec 30 '22 at 08:52

1 Answers1

0

First I have to say that the fragment is special class it's instantiation must be done by OS, By adding it to your module you round the life cycle of fragment which must be control by OS.

https://google.github.io/dagger/android

However if you wish to provide it by module you have to use Qualifiers for generic types. Change your module as following:

 @Module
public abstract class BindingModule {

    @ContributesAndroidInjector(modules = MyFragmentModule.class)
    @Name('YourTypeName')
    abstract MyFragment<YourType> bindMyFragment();  
} 

So wherever you what to inject it, you have to specify the type and it's Qualifier:

@Inject
@Name('YourTypeName')
MyFragment<YourType> fragmentReference;
Mahdi Rajabi
  • 582
  • 1
  • 4
  • 11
  • I didn't notice any reference to provide ```Fragment``` as a dependency in the origin post. He or she just use relatively new mechanism to bind component with dependencies. The question was how to use this mechanism for fragments that exploit generics. – Joe Dow Dec 30 '22 at 08:49