I'm working on an android application and I'm trying to inject a field which is type parameterized in an abstract class : BaseListFragment
public abstract class BaseListFragment<E, A extends ArrayAdapter<E>, S> extends BaseFragment
{
@Inject protected S service;
}
But I get this following error at compile : error: cannot find symbol class S
Here is my code for BaseFragment :
public class BaseFragment extends Fragment
{
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
((App) getActivity().getApplication()).inject(this);
}
}
here is my service module :
@Module(
complete = false,
library = true
)
public class ServiceModule
{
@Provides @Singleton BucketService provideBucketService(RestAdapter restAdapter)
{
return restAdapter.create(BucketService.class);
}
@Provides @Singleton ProjectService provideProjectService(RestAdapter restAdapter)
{
return restAdapter.create(ProjectService.class);
}
@Provides @Singleton ShotService provideShotService(RestAdapter restAdapter)
{
return restAdapter.create(ShotService.class);
}
@Provides @Singleton TeamService provideTeamService(RestAdapter restAdapter)
{
return restAdapter.create(TeamService.class);
}
@Provides @Singleton UserService provideUserService(RestAdapter restAdapter)
{
return restAdapter.create(UserService.class);
}
}
And here is an example of a class extending BaseListFragment :
public class ProjectFragment extends BaseEntityFragment<Project, ProjectViewAdapter, ProjectService>
{
}
Is there anyway to inject a parameterized type ?
Regards,
Dagger constructs instances of your application classes and satisfies their dependencies. It uses the javax. inject. Inject annotation to identify which constructors and fields it is interested in. Use @Inject to annotate the constructor that Dagger should use to create instances of a class.
Inject values at runtime with UI in Dagger2:pureMathModule("Book Name") . build() . inject(this); The difference between DaggerComponent create() and in build() is - create() works when no runtime argument is passed into the constructor, else we use build() method.
Dagger is a fully static, compile-time dependency injection framework for Java, Kotlin, and Android. It is an adaptation of an earlier version created by Square and now maintained by Google.
Dagger automatically generates code that mimics the code you would otherwise have hand-written. Because the code is generated at compile time, it's traceable and more performant than other reflection-based solutions such as Guice. Note: Use Hilt for dependency injection on Android.
As of the 2.0.1 release, Dagger 2 does support the type of injection that you're talking about. Take a look at GenericTest
for all of the various permutations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With