[英]Understanding Android Architecture Components example GithubBrowserSample: ViewModelModule, ViewModel parameters
One of the most up to date samples covering Android Architecture Components is GithubBrowserSample provided by Google. 涵盖Android架构组件的最新样本之一是Google提供的GithubBrowserSample 。 I reviewed the code and a few questions arose: 我查看了代码并出现了一些问题:
I have noticed that ViewModelModule is included in AppModule . 我注意到ViewModelModule包含在AppModule中 。 It means that all the viewmodels are added to the DI graph. 这意味着所有视图模型都被添加到DI图中。 Why that is done in that way instead of having separate Module
for each Activity/Fragment that would provide only needed ViewModel
for specific Activity/Fragment? 为什么以这种方式完成,而不是为每个Activity / Fragment提供单独的Module
,只为特定的Activity / Fragment提供所需的ViewModel
?
In this specific example, where viewmodels are instantiated using GithubViewModelFactory is there any way to pass a parameter to the specific ViewModel
? 在这个具体的例子中,使用GithubViewModelFactory实例化视图模型有没有办法将参数传递给特定的ViewModel
? Or the better solution would be to create a setter in ViewModel
and set needed param via setter? 或者更好的解决方案是在ViewModel
创建一个setter并通过setter设置所需的param?
- [...] It means that all the viewmodels are added to the DI graph. [...]这意味着所有视图模型都被添加到DI图中。 Why that is done in that way instead of having separate Module for each Activity/Fragment [...]? 为什么以这种方式完成而不是为每个Activity / Fragment [...]分别设置模块?
They are added to the DI graph, but they are not yet created. 它们被添加到DI图中,但尚未创建。 Instead they end up in a map of providers, as seen in the ViewModelFacory . 相反,它们最终出现在提供者的地图中,如ViewModelFacory中所示 。
@Inject
public GithubViewModelFactory(Map<Class<? extends ViewModel>, Provider<ViewModel>> creators) { }
So we now have a GithubViewModelFactory
that has a list of providers and can create any ViewModel
that was bound. 所以我们现在有一个GithubViewModelFactory
,它有一个提供者列表,可以创建任何绑定的ViewModel
。 Fragments and Activities can now just inject the factory and retrieve their ViewModel. 片段和活动现在可以注入工厂并检索他们的ViewModel。
@Inject
ViewModelProvider.Factory viewModelFactory;
// ...later...
repoViewModel = ViewModelProviders.of(this, viewModelFactory).get(RepoViewModel.class);
As to the why...alternatively you could create a ViewModelProvider.Factory
for every Activity / Fragment and register the implementation in every Module. 至于为什么......或者你可以为每个Activity / Fragment创建一个ViewModelProvider.Factory
,并在每个Module中注册实现。 This would be a lot of duplicated boilerplate code, though. 但是,这将是许多重复的样板代码。
- In this specific example, where viewmodels are instantiated using GithubViewModelFactory is there any way to pass a parameter to the specific ViewModel? 在这个具体的例子中,使用GithubViewModelFactory实例化视图模型有没有办法将参数传递给特定的ViewModel? Or the better solution would be to create a setter in ViewModel and set needed param via setter? 或者更好的解决方案是在ViewModel中创建一个setter并通过setter设置所需的param?
It seems like all the ViewModels only depend on @Singleton
objects—which is necessary, since they all get provided from the AppComponent. 似乎所有ViewModel都只依赖于@Singleton
对象 - 这是必要的,因为它们都是从AppComponent提供的。 This means that there is no way to pass in "parameters" other than other @Singleton
dependencies. 这意味着除了其他@Singleton
依赖项之外,无法传入“参数”。
So, as you suggested, you'd either have to move the factory down into the Activity / Fragment component so that you can provide lower-scoped dependencies, or use a setter method. 因此,正如您所建议的那样,您必须将工厂向下移动到Activity / Fragment组件中,以便您可以提供较低范围的依赖项,或使用setter方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.