简体   繁体   English

传递“这个”和“活动!!”有什么区别? 在创建 ViewModelProvider 实例时作为 ViewModelStoreOwner

[英]What is the difference between passing“this” and “activity!!” as a ViewModelStoreOwner while creating ViewModelProvider instance

Hi I am a Kotlin learner, wanted to understand the difference between passing"this" and "activity!!"嗨,我是 Kotlin 学习者,想了解传递“这个”和“活动!!”之间的区别! as a ViewModelStoreOwner while creating ViewModelProvider instance in a fragment for ex作为 ViewModelStoreOwner 在片段中为 ex 创建 ViewModelProvider 实例

 viewModel = ViewModelProvider(
        this,
        InventoryDetailsFragmentViewModelFactory.getInstance(activity!!.application)
    )
        .get(InventoryDetailsFragmentViewModel::class.java)

when I am using this as owner sometimes observer is not working Please help me to understand difference in using this and activity!!当我将其用作所有者时,有时观察者无法正常工作请帮助我了解使用此功能和活动的区别!

You are able to pass either this (a Fragment ) or activity!!您可以通过thisFragment )或activity!! (a FragmentActivity ) to the ViewModelProvider constructor because both implement the ViewModelStoreOwner interface. (一个FragmentActivity )到ViewModelProvider构造函数,因为它们都实现ViewModelStoreOwner接口。

The role of a ViewModelStoreOwner is to be able to provide a ViewModelStore , when needed, where the ViewModelStore represents a collection of existing viewmodels: ViewModelStoreOwner的作用是能够在需要时提供ViewModelStore ,其中ViewModelStore代表现有视图模型的集合:

  • If you use this and pass a Fragment to the ViewModelProvider constructor, the ViewModelStore will be tied to that Fragment .如果您使用this并将Fragment传递给ViewModelProvider构造函数,则ViewModelStore将绑定到该Fragment This fragment and child fragments might share viewmodels, but those viewmodels should not be shared with other peer fragments or parents.该片段和子片段可能共享视图模型,但这些视图模型不应与其他对等片段或父片段共享。

  • If you use activity!!如果您使用activity!! and pass a FragmentActivity to the ViewModelProvider constructor, the ViewModelStore will be tied to that FragmentActivity .并将FragmentActivity传递给ViewModelProvider构造函数, ViewModelStore将绑定到该FragmentActivity Not only can this activity use the viewmodel, but any fragments used in that activity could also share that viewmodel.该活动不仅可以使用视图模型,而且该活动中使用的任何片段也可以共享该视图模型。

You need to decide what the proper scope is of your InventoryDetailsFragmentViewModel .您需要确定InventoryDetailsFragmentViewModel tailsFragmentViewModel 的正确 scope 是什么。

"this" means "Context", which is the Fragment you are Instantiated the viewModel. “this”的意思是“Context”,也就是你实例化viewModel的Fragment。

From the code you provided i can understand "this" = "InventoryDetailsFragment", which is >the class you instantiated the viewModel.从您提供的代码中,我可以理解“this”=“InventoryDetailsFragment”,即>您实例化 viewModel 的 class。

Your code should look like this您的代码应如下所示

val application = requireNotNull(activity).application
viewModelFactory = InventoryDetailsFragmentViewModelFactory(application)
viewModel = ViewModelProviders.of(this, viewModelFactory)
            .get(InventoryDetailsFragmentViewModel::class.java)

If you want to see an example where ViewModel and ViewModelFactory are used in a project visit this link sample如果您想查看在项目中使用 ViewModel 和 ViewModelFactory 的示例,请访问此链接示例

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

相关问题 片段中的 ViewModelProvider 使用什么 ViewModelStoreOwner? - What ViewModelStoreOwner to use for ViewModelProvider in Fragment? ViewModelProvider 构造函数有什么区别 - what is the difference between ViewModelProvider constructors ViewModelProviders 和 ViewModelProvider 类有什么区别? - What is the difference between ViewModelProviders and ViewModelProvider class? ViewModelProvider.Factory 和 ViewModelProvider.NewInstanceFactory 有什么区别? - What are the differences between ViewModelProvider.Factory and ViewModelProvider.NewInstanceFactory? 在getApplicationContext()或Activity之间使用上下文创建Intent有何区别? - What's the difference for creating an Intent with context between getApplicationContext() or activity 什么是 ViewModelStore 和 viewModelStoreOwner? - What is ViewModelStore and viewModelStoreOwner? 布局和活动有什么区别? - What is the difference between layout and activity? Activity 和 Context 有什么区别? - What is the difference between Activity and Context? FragmentActivity和Activity之间有什么区别? - What is difference between FragmentActivity and Activity? 如何在活动之间共享 ViewModelProvider.Factory 实例 - How to share a ViewModelProvider.Factory instance between Activities
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM