如何正确确定 ViewModel 的范围? [英] How to scope ViewModels properly?

查看:27
本文介绍了如何正确确定 ViewModel 的范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试围绕新的 Android 架构组件,特别是 ViewModel.

I am trying to wrap my head around the new Android Architecture Components, specifically the ViewModel.

我的印象是一个Fragment不应该知道它属于什么Activity或Fragment,这样才能在应用的不同上下文中使用.通过直接在 Fragment 中声明 ViewModel 范围,而不是在 Fragment 所有者中,这些示例似乎与此相矛盾:

My impression is that a Fragment should not know about what Activity or Fragment it is owned by, so that it can be used in different contexts of the application. The examples seem to contradict this by declaring the ViewModel scope directly in the Fragment, rather than the Fragment owner:

viewModel = ViewModelProviders.of(getActivity()).get(SomeViewModel.class);

我希望能够在 Master/Detail 配置中使用此 Fragment,其中两者共享相同的状态(即 ViewModel 实例),以及在 ViewPager 中,其中所有 Fragment 都需要单独的状态(即单独的 ViewModel 实例)).我希望 Fragment 所有者指定范围,但这种方法似乎不支持.

I would like to be able to use this Fragment in a Master/Detail configuration, where both share the same state (i.e. ViewModel instance), as well as inside a ViewPager, where all Fragments require separate state (i.e. separate ViewModel instances). I would expect the Fragment owner to dictate the scope, which does not seem to be supported with this approach.

我想出的解决方法可能是在必要时(例如在主/详细信息配置中)有选择地传入已配置的 ViewModelProvider 以显式扩大 ViewModel 的范围,并且默认情况下具有单独的 ViewModel:

A workaround I came up with could be to optionally pass in a configured ViewModelProvider to explicitly broaden the scope of a ViewModel if necessary (e.g. in the Master/Detail configuration) and have separate ViewModels by default:

final ViewModelProvider viewModelProvider;
if (viewModelProviderOverride != null) {
    viewModelProvider = viewModelProviderOverride;
} else {
    viewModelProvider = ViewModelProviders.of(this);
}

viewModel = viewModelProvider.get(SomeViewModel.class);

这样,所有者可以决定多个 Fragment 是否共享它们的状态.我还没有完全考虑清楚,但这似乎有点可疑,因为文档中似乎没有暗示任何接近它的内容.我觉得我遗漏了一些明显的东西.

This way, the owner can decide whether multiple Fragments will share their state or not. I haven't fully thought this through yet, but it seems kind of fishy, since nothing close to it seems to be hinted at in the docs. I feel like I am missing something obvious.

推荐答案

viewModel = ViewModelProviders.of(getActivity()).get(SomeViewModel.class); 表示你想要状态ViewModel 的范围限定为 Activity.如果您不希望将 Fragment 限定在活动范围内,您可以调用 viewModel = ViewModelProviders.of(this).get(SomeViewModel.class);.现在你要做的就是 if (shareState) 然后调用第一个,如果不调用第二个.

Saying viewModel = ViewModelProviders.of(getActivity()).get(SomeViewModel.class); means that you would like the state of the ViewModel to be scoped to the Activity. If you would not like the Fragment to be scoped to the activity you can just call viewModel = ViewModelProviders.of(this).get(SomeViewModel.class);. Now all you do is if (shareState) then call the first and if not call the second.

这篇关于如何正确确定 ViewModel 的范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆