简体   繁体   English

片段中的 ViewModelProvider 使用什么 ViewModelStoreOwner?

[英]What ViewModelStoreOwner to use for ViewModelProvider in Fragment?

I've creating a test activity that updates some text in my MyViewModel.我创建了一个测试活动来更新 MyViewModel 中的一些文本。

I'd like to observe these changes in a Fragment , but when I use我想在Fragment中观察这些变化,但是当我使用

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

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.它给了我一个与活动中使用的不同的 MyViewModel 实例,这导致我在片段中的onChanged()回调没有被调用。

Only when I modify that same fragment code to只有当我将相同的片段代码修改为

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

does the fragment get the same instance of MyViewModel as the activity - so onChanged() is successfully called.片段是否获得与活动相同的 MyViewModel 实例 - 所以onChanged()被成功调用。

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.但是,我不确定使用getActivity()作为 ViewModelStoreOwner 是否是正确的做事方式,因为我在任何地方的任何示例中都没有看到这一点。 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?我想知道在这种情况下是否应该使用更好的 ViewModelStoreOwner ?

You should use activity instance for sharing the same instance among fragments in the same activity.您应该使用activity实例在同一活动中的片段之间共享同一实例。

Both Activity and Fragment implements their own ViewModelStoreOwner interface and implements the getViewModelStore() method. Activity 和Fragment都实现了自己的ViewModelStoreOwner接口,并实现了getViewModelStore()方法。 getViewModelStore() provide the ViewModelStore instance which is used to store the viewmodel objects, created by the ViewModelProvider . getViewModelStore()提供ViewModelStore实例,该实例用于存储由viewmodel创建的视图ViewModelProvider对象。

Note: ComponentActivity implements the ViewModelStoreOwner interface and FragmentActivity (parent of AppCompatActivity) inherits the implementation.注意: ComponentActivity实现ViewModelStoreOwner接口, FragmentActivity (AppCompatActivity 的父级)继承实现。

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).因此 Activity 和 Fragment 都有ViewModelStoreOwner接口方法的特定实现,并根据对象的lifecycle (包括配置更改)存储 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.由于片段属于活动,因此获取相同的活动实例,因此使用getActivity()将导致使用活动的ViewModelStoreOwner object。 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).要在片段之间共享对象,只需使用活动实例创建ViewModelProvider ,它将在所有片段中使用相同的ViewModelStoreOwner ,因此将返回视图模型的持久viewmodel (如果之前创建)。

Having an Activity which does as little as possible has become a "best practice" for some time now, so the scenario of an Activity and a Fragment which need access to the same ViewModel instance may not be covered by many guides.一段时间以来,拥有尽可能少的Activity已成为“最佳实践”,因此许多指南可能不会涵盖需要访问同一ViewModel实例的ActivityFragment的场景。

But "there is no rule without an exception", and your scenario is similar to the one where an Activity has two Fragment s which need to share data.但是“没有例外,没有规则”,您的场景类似于Activity有两个Fragment需要共享数据的场景。

In this case, one uses the Activity scope for the ViewModel to make sure every component will have access to the same instance.在这种情况下,可以为ViewModel使用Activity scope 以确保每个组件都可以访问同一个实例。 See also the section "Share data between fragments" in the View Model Overview at developer.android.com另请参阅developer.android.com的视图 Model 概述中的“片段之间共享数据”部分

I think you are getting error not because of your ViewModel class.我认为您收到错误不是因为您的 ViewModel class。 I erased all the codes except the constructor and just kept a simple class.我删除了除构造函数之外的所有代码,只保留了一个简单的 class。 It works fine.它工作正常。 If you are following MVVM pattern then, the you may get error because of the database implementation or you may check the repository.如果您遵循 MVVM 模式,那么您可能会因为数据库实现而出错,或者您可能会检查存储库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 传递“这个”和“活动!!”有什么区别? 在创建 ViewModelProvider 实例时作为 ViewModelStoreOwner - What is the difference between passing“this” and “activity!!” as a ViewModelStoreOwner while creating ViewModelProvider instance 什么是 ViewModelStore 和 viewModelStoreOwner? - What is ViewModelStore and viewModelStoreOwner? ViewModelProvider Fragment 实例化 model - ViewModelProvider Fragment instantiate model ViewModelProvider 中的 androidx.lifecycle.ViewModelStoreOwner 不能应用于 androidx.lifecycle.LifeCycleOwner - androidx.lifecycle.ViewModelStoreOwner in ViewModelProvider cannot be applied to androidx.lifecycle.LifeCycleOwner 无法解析片段中的 ViewModelProvider 构造? - Cannot resolve ViewModelProvider construction in a fragment? 片段中的 ViewmodelProvider 上下文类型不匹配 - Viewmodelprovider context type missmatch in fragment ViewModelProvider 构造函数有什么区别 - what is the difference between ViewModelProvider constructors 在 VIew 中如何使用 ViewModel 和 ViewModelProvider - In VIew how to use ViewModel with ViewModelProvider 如何将 ViewModelProvider.Factory 注入片段 - How to inject ViewModelProvider.Factory into a fragment ViewModelProvider.Factory 和 ViewModelProvider.NewInstanceFactory 有什么区别? - What are the differences between ViewModelProvider.Factory and ViewModelProvider.NewInstanceFactory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM