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

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

问题描述

我正在努力围绕新的Android体系结构组件,特别是ViewModel

我的印象是,一个Fragment不应知道它所拥有的Activity或Fragment,以便可以在应用程序的不同上下文中使用它.通过直接在Fragment中而不是Fragment所有者中声明ViewModel范围,这些示例似乎与此矛盾:

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

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

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

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

viewModel = viewModelProvider.get(SomeViewModel.class);

这样,所有者可以决定是否多个片段将共享其状态.我还没有完全考虑到这一点,但是似乎有点可疑,因为文档中似乎没有暗示任何与之接近的东西.我觉得我缺少明显的东西.

解决方案

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

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

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);

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.

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);

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.

解决方案

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天全站免登陆