Trying to learn Dagger2 and using Dagger2 2.14.1. I can't get past:
no suitable method found for inject(SettingsFragment)
SettingsFragment is:
public class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener {
....
@Override
public void onAttach(Context context){
AndroidInjection.inject(this); <<<< Error here
super.onAttach(context);
}}
as I can't extend PreferenceFragmentCompat from DaggerFragment.
My builders module is
@Module(subcomponents = {SettingsFragmentSubComponent.class, StartupPhotoFragmentSubComponent.class })
public abstract class UIBuildersModule {
@Binds
@IntoMap
@FragmentKey(SettingsFragment.class)
abstract AndroidInjector.Factory<? extends Fragment> bindSettingsFragmentInjectorFactory(SettingsFragmentSubComponent.Builder builder);
// @ContributesAndroidInjector <<< tried also without luck.
// abstract SettingsFragment bindSettingsFragment();
@ContributesAndroidInjector
abstract StartupPhotoFragment bindStartupPhotoFragment();
I did create module and subcomponent for SettingsFragment which are really empty as I want to get dependencies that I created at app level:
@Module
public class SettingsFragmentModule {
}
@Subcomponent(modules = {SettingsFragmentModule.class})
public interface SettingsFragmentSubComponent extends
AndroidInjector<SettingsFragment> {
@Subcomponent.Builder
abstract class Builder extends AndroidInjector.Builder<SettingsFragment> {
}}
Edit: Added ApplicationComponent that includes UIBuildersModule, and snippet from build.gradle
@Singleton
@Component(modules = {UIBuildersModule.class,
AndroidSupportInjectionModule.class, ApplicationModule.class, })
public interface ApplicationComponent extends
AndroidInjector<ActivityTrackerApplication> {
@Component.Builder
interface Builder {
@BindsInstance
Builder application(ActivityTrackerApplication application);
ApplicationComponent build();
}
void inject(ActivityTrackerApplication activityTrackerApplication);
void inject(StartupPhotoFragment startupPhotoFragment);
void inject(SettingsFragment settingsFragment);
}
Also here is snippet from build.gradle
final DAGGER_VERSION="2.14.1"
....
// For Dagger2
compile "com.google.dagger:dagger:$DAGGER_VERSION"
compile "com.google.dagger:dagger-android:$DAGGER_VERSION"
compile "com.google.dagger:dagger-android-support:$DAGGER_VERSION"
annotationProcessor "com.google.dagger:dagger-android-processor:$DAGGER_VERSION"
annotationProcessor "com.google.dagger:dagger-compiler:$DAGGER_VERSION"
EDIT2: Solution Ahh - but of course.
public class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener {
....
@Override
public void onAttach(Context context){
AndroidSupportInjection.inject(this); <<<<Needed AndroidSupportInjection
super.onAttach(context);
}}