在Fragment中为ViewModelProvider使用哪个ViewModelStoreOwner? [英] What ViewModelStoreOwner to use for ViewModelProvider in Fragment?

查看:1849
本文介绍了在Fragment中为ViewModelProvider使用哪个ViewModelStoreOwner?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个测试活动,以更新MyViewModel中的一些文本.

I've creating a test activity that updates some text in my MyViewModel.

我想在片段中观察这些变化,但是当我使用

I'd like to observe these changes in a Fragment, but when I use

MyViewModel myViewModel = new ViewModelProvider(this).get(MyViewModel.class);

它为我提供的MyViewModel实例与活动中使用的实例不同,这导致片段中的onChanged()回调未被调用.

it gives me a different instance of MyViewModel than that used in the activity, which results in my onChanged() callback in the fragment not being called.

仅当我将相同的片段代码修改为

Only when I modify that same fragment code to

HomeViewModel homeViewModel = new ViewModelProvider(getActivity()).get(HomeViewModel.class);

该片段是否获得与活动相同的MyViewModel实例-因此成功调用了onChanged().

does the fragment get the same instance of MyViewModel as the activity - so onChanged() is successfully called.

但是,我不确定使用getActivity()作为ViewModelStoreOwner是否是正确的处理方式,因为我在任何地方的示例中都没有看到这一点.我想知道在这种情况下是否应该使用更好的ViewModelStoreOwner?

However, I'm not sure if using getActivity() as the ViewModelStoreOwner is the proper way of doing things as I haven't seen this in any examples anywhere. I'm wondering if there might be a better ViewModelStoreOwner I should be using in this instance?

推荐答案

我想知道是否应该有更好的ViewModelStoreOwner 在这种情况下使用?

I'm wondering if there might be a better ViewModelStoreOwner I should be using in this instance?

您应该使用activity实例在同一活动中的片段之间共享同一实例.

You should use activity instance for sharing the same instance among fragments in the same activity.

Activity和 Fragment 都实现了自己的 ViewModelStoreOwner 接口并实现getViewModelStore()方法. getViewModelStore()提供ViewModelStore实例,该实例用于存储由ViewModelProvider创建的viewmodel对象.

Both Activity and Fragment implements their own ViewModelStoreOwner interface and implements the getViewModelStore() method. getViewModelStore() provide the ViewModelStore instance which is used to store the viewmodel objects, created by the ViewModelProvider.

注意: ComponentActivity 实现ViewModelStoreOwner接口和FragmentActivity (AppCompatActivity的父级)继承实现.

Note: ComponentActivity implements the ViewModelStoreOwner interface and FragmentActivity (parent of AppCompatActivity) inherits the implementation.

因此,Activity和Fragment对ViewModelStoreOwner接口方法都有特定的实现,并根据对象的lifecycle(包括配置更改)存储viewmodel实例.

So both Activity and Fragment have specific implementation for the ViewModelStoreOwner interface methods and store the viewmodel instance as per the lifecycle of the objects(including the configuration changes).

由于属于活动的片段会获得相同的活动实例,因此使用getActivity()将导致使用活动的ViewModelStoreOwner对象.要在片段之间共享对象,只需使用活动实例来创建ViewModelProvider,该实例将在所有片段中使用相同的ViewModelStoreOwner,因此将返回持久保存的viewmodel对象(如果之前已创建).

Since fragments belong to activity get the same activity instance so using the getActivity() will result in using the ViewModelStoreOwner object of the activity. To share the objects among fragments, simply use the activity instance for creating ViewModelProvider which will use the same ViewModelStoreOwner in all fragments hence will return the persisted object of viewmodel (if created before).

这篇关于在Fragment中为ViewModelProvider使用哪个ViewModelStoreOwner?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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