В соответствии с http://konmik.github.io/snorkeling-with-dagger-2.html я могу просто добавить
inject(Anything anything)
в AppComponent.java, но это не работает для меня, в примере статей:
@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {
void inject(MainActivity activity);
void inject(MainFragment fragment);
void inject(MainToolbarView view);
}
Если я пытаюсь ввести зависимости в свой фрагмент, введенные члены остаются пустыми. Какую очевидную ошибку я не вижу здесь?
Релевантные источники:
ApplicationComponent.java
@Singleton
@Component(
modules = ApplicationModule.class
)
public interface ApplicationComponent {
void injectApplication(BaseApplication application);
Prefs providePrefs();
}
ApplicationModule.java
@Module
public class ApplicationModule {
private final Application application;
public ApplicationModule(Application application) {
this.application = application;
}
@Provides
Application application() {
return application;
}
@Singleton
@Provides
Prefs providePrefs() {
return Prefs.with(application);
}
}
ActivityComponent.java
@AScope
@Component(
dependencies = {
ApplicationComponent.class
},
modules = ActivityModule.class
)
public interface ActivityComponent extends ApplicationComponent {
void injectActivity(BaseActivity activity);
void injectFragment(BaseFragment fragment);
}
ActivityModule.java
@Module
public class ActivityModule {
private final Activity activity;
public ActivityModule(Activity activity) {
this.activity = activity;
}
@Provides
Activity activity() {
return activity;
}
@Provides
Context context() {
return activity;
}
}
BaseApplication.java
public class BaseApplication extends Application {
private ApplicationComponent component;
@Override
public void onCreate() {
super.onCreate();
component = createComponent();
component.injectApplication(this);
}
private ApplicationComponent createComponent() {
return Dagger_ApplicationComponent.builder()
.applicationModule(new ApplicationModule(this))
.build();
}
public ApplicationComponent getComponent() {
return component;
}
}
BaseActivity.java
public class BaseActivity extends ActionBarActivity implements HasComponent<ActivityComponent> {
ActivityComponent component;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
component = Dagger_ActivityComponent.builder()
.applicationComponent(((BaseApplication) getApplication()).getComponent())
.activityModule(new ActivityModule(this))
.build();
component.injectActivity(this);
}
@Override
public ActivityComponent getComponent() {
return component;
}
}
BaseFragment.java
public class BaseFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((BaseActivity)getActivity()).getComponent().injectFragment(this);
}
}
ListFragment.java
public class ListFragment extends BaseFragment {
@Inject ListFragmentPresenterImpl listFragmentPresenter;
public static ListFragment newInstance(){
ListFragment result = new ListFragment();
return result;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.list_fragment, container, false);
Log.d("",listFragmentPresenter.sayHello()); // NPE HERE
return rootView;
}
}
ListFragmentPresenterImpl.java
public class ListFragmentPresenterImpl implements ListFragmentPresenter {
@Inject
public ListFragmentPresenterImpl() {
}
@Override
public String sayHello() {
return "hello";
}
}
В Dagger_ActivityComponent сгенерированный код выглядит следующим образом:
@Override
public void injectActivity(BaseActivity activity) {
baseActivityMembersInjector.injectMembers(activity);
}
@Override
public void injectFragment(BaseFragment fragment) {
MembersInjectors.noOp().injectMembers(fragment);
}
здесь не должен быть baseFragmentMemebersInjector?
Спасибо!