[英]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!!
您可以通过
this
( Fragment
)或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.