i有一个名为viewModelModule的模块。就是这样:
@Module(includes = RepositoryModule.class)
public class ViewModelModule {
StepListFragment stepListFragment;
StepViewFragment stepViewFragment;
@Provides
public StepListFragment getStepListFragment() {
return stepListFragment;
}
@Provides
public StepViewFragment getStepViewFragment() {
return stepViewFragment;
}
public ViewModelModule(StepListFragment stepListFragment) {
this.stepListFragment = stepListFragment;
}
public ViewModelModule(StepViewFragment stepViewFragment) {
this.stepViewFragment = stepViewFragment;
}
@Provides
IngredientsViewModel ingredientsViewModel(StepListFragment stepListFragment, RecipesRepository repository) {
return ViewModelProviders.of(stepListFragment, new ViewModelProvider.Factory() {
@Override
public <T extends ViewModel> T create(Class<T> modelClass) {
return (T) new IngredientsViewModel(repository);
}
}).get(IngredientsViewModel.class);
}
@Provides
StepsViewModel stepsViewModel(StepViewFragment stepViewFragment, RecipesRepository repository) {
return ViewModelProviders.of(stepViewFragment, new ViewModelProvider.Factory() {
@Override
public <T extends ViewModel> T create(Class<T> modelClass) {
return (T) new StepsViewModel(repository);
}
}).get(StepsViewModel.class);
}
}
将提供我所有的ViewModules组件。但不是同时。
我有一个针对每个片段的组件:
@Component(modules = {RepositoryModule.class, ContextModule.class, ViewModelModule.class})
public interface StepListComponent {
void inject (StepListFragment stepListFragment);
}
@Component(modules = {RepositoryModule.class, ContextModule.class, ViewModelModule.class})
public interface StepViewComponent {
void inject (StepViewFragment stepViewFragment);
}
在第一个时刻显示了阶段段,我将组件实例化如下:
DaggerStepListComponent.builder()
.applicationModule(new ApplicationModule(this.getActivity().getApplication()))
.contextModule(new ContextModule(this.getActivity()))
.viewModelModule(new ViewModelModule(this)).build().inject(this);
正如您在条款的末尾看到的那样,我注入了片段。
之后,当我启动另一个片段时,我会做同样的事情。但是当它调用上面的代码时,我收到此错误:
Caused by: java.lang.NullPointerException: Cannot return null from a non-@Nullable @Provides method
当然,错误原因是我尚未实例化StepViewFragment stepViewFragment
;但是我现在不想使用它,所以不会引起问题。
我尝试添加@Nullable
子句如下:
@Nullable
@Provides
public StepListFragment getStepListFragment() {
return stepListFragment;
}
@Nullable
@Provides
public StepViewFragment getStepViewFragment() {
return stepViewFragment;
}
,但是我得到了一个编译时间错误:
Error:(15, 10) error: StepListFragment is not nullable, but is being provided by @android.support.annotation.Nullable @Provides
com.github.alexpfx.udacity.nanodegree.android.baking_app.step.master.steps.StepListFragment com.github.alexpfx.udacity.nanodegree.android.baking_app.step.master.di.ViewModelModule.getStepListFragment()
at: com.github.alexpfx.udacity.nanodegree.android.baking_app.step.master.steps.StepListFragment is injected at
com.github.alexpfx.udacity.nanodegree.android.baking_app.step.master.di.ViewModelModule.ingredientsViewModel(stepListFragment, …)
com.github.alexpfx.udacity.nanodegree.android.baking_app.step.master.ingredients.IngredientsViewModel is injected at
com.github.alexpfx.udacity.nanodegree.android.baking_app.step.master.steps.StepListFragment.ingredientsViewModel
com.github.alexpfx.udacity.nanodegree.android.baking_app.step.master.steps.StepListFragment is injected at
com.github.alexpfx.udacity.nanodegree.android.baking_app.step.master.di.StepListComponent.inject(stepListFragment)
因此,问题是:是否有一种方法可以通过使用相同的模块保持配置来解决此问题,还是我应该将每个@provides分离到其各自的模块中?这是一个好习惯吗?
片段不应是 ViewModel
的依赖项 - ViewModel
应该比Fragment
的范围更大。
请参阅此GitHub回购,其中包含一个示例项目,该项目将Android architecture components
与Dagger2
一起使用。