简体   繁体   English

ActivityViewModels 和惰性 ViewModelProvider 之间的区别?

[英]Difference between ActivityViewModels and lazy ViewModelProvider?

Difference between ActivityViewModels and lazy ViewModelProvider? ActivityViewModels 和惰性 ViewModelProvider 之间的区别?

I've seen 2 ways to initialize a viewmodel:我见过两种初始化视图模型的方法:

private val someViewModel: SomeViewModel by activityViewModels()
private val someOtherViewModel: SomeOtherViewModel by lazy {
        ViewModelProvider(this).get(SomeOtherViewModel::class.java)
}

I know lazy initializes the ViewModel only when it's needed and that activityViewModels is useful for sharing data between fragments, but other than that what's the difference?我知道lazy仅在需要时才初始化 ViewModel,并且activityViewModels对于在片段之间共享数据很有用,但除此之外有什么区别?

Android documentation says the activityViewModels is scoped to the activity, but does that mean if the same viewmodel is used in multiple fragments within the same activity using activityViewModels that there's only one instance created that's shared between all the fragments? Android 文档说activityViewModels的范围仅限于活动,但这是否意味着如果在使用activityViewModels的同一活动中的多个片段中使用相同的视图模型,那么只会创建一个在所有片段之间共享的实例?

When you call ViewModelProvider(this) , this refers to the ViewModelStoreOwner .当您调用ViewModelProvider(this)时, this指的是ViewModelStoreOwner

For each unique ViewModelStoreOwner, there will be a unique ViewModel of the given type .对于每个唯一的 ViewModelStoreOwner,将有一个给定type的唯一 ViewModel。

Now coming to the question.现在来回答这个问题。

When you call你打电话时

private val someOtherViewModel: SomeOtherViewModel by lazy {
  ViewModelProvider(this).get(SomeOtherViewModel::class.java)
}

you are getting a ViewModel that is scoped to the current Fragment/Activity.您将获得一个适用于当前片段/活动的 ViewModel。 Lazy just defers the initialization. Lazy只是推迟了初始化。

When you call你打电话时

private val someViewModel: SomeViewModel by activityViewModels()

you are getting a ViewModel that is scoped to the Activity.您将获得一个仅限于 Activity 的 ViewModel。 When multiple fragments use the same code, they are requesting ViewModels scoped to the parent Activity.当多个片段使用相同的代码时,它们会请求限定在父 Activity 范围内的 ViewModel。 If the parent Activity for all the Fragments is the same, the Fragments will get the same ViewModel since the ViewModelStoreOwner connected to the Activity remains the same.如果所有 Fragment 的父 Activity 都相同,则 Fragment 将获得相同的 ViewModel,因为连接到 Activity 的ViewModelStoreOwner保持不变。

A current version of this is replace这个的当前版本是替换

private val viewModel by lazy { ViewModelProvider(this)[TaskTimerViewModel::class.java] }

with

private val viewModel: TaskTimerViewModel by activityViewModels()

The former just differs the initialization but creates a viewModel instance for each Fragment.前者只是初始化不同,而是为每个 Fragment 创建一个 viewModel 实例。 This works but creates more work, the query to get the data is run for each Fragment.这行得通,但会增加工作量,获取数据的查询会针对每个片段运行。

The latter gets the ViewModel that scopes to the activity.后者获取活动范围内的 ViewModel。 It will get the same view model for multiple Fragments that are in the same activity.对于同一活动中的多个片段,它将获得相同的视图 model。

TaskTimerViewModel was created with TaskTimerViewModel 是用

class TaskTimerViewModel(application: Application) : AndroidViewModel(application) {

But the fact that it used AndroidViewModel did not seem to cause a problem.但它使用 AndroidViewModel 的事实似乎并没有引起问题。

Thanks for the previous answer, it was spot on when it was created and very clear.感谢您之前的回答,它是在创建时出现的并且非常清晰。 Hopefully this update is of some use to someone.希望此更新对某些人有用。

use by activityViewModels() when you have 1-shared code in ViewModel used by many fragments 2-these fragments had the same parent Activity.由 activityViewModels() 使用,当您在 ViewModel 中有被许多片段使用的共享代码时 1-这些片段具有相同的父 Activity。

-So if you don't have this scenario use normal viewModel Code Ex:- private lateinit var viewModel: YourViewModelClass viewModel = ViewModelProvider(this)[YourViewModelClass::class.java] -因此,如果您没有这种情况,请使用普通的 viewModel 代码示例:- private lateinit var viewModel: YourViewModelClass viewModel = ViewModelProvider(this)[YourViewModelClass::class.java]

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

相关问题 navGraphViewModels 和 activityViewModels 有什么区别? - What is the difference between navGraphViewModels and activityViewModels? ViewModelProvider 构造函数有什么区别 - what is the difference between ViewModelProvider constructors ViewModelProviders 和 ViewModelProvider 类有什么区别? - What is the difference between ViewModelProviders and ViewModelProvider class? 传递“这个”和“活动!!”有什么区别? 在创建 ViewModelProvider 实例时作为 ViewModelStoreOwner - What is the difference between passing“this” and “activity!!” as a ViewModelStoreOwner while creating ViewModelProvider instance ViewModelProvider.Factory 和 ViewModelProvider.NewInstanceFactory 有什么区别? - What are the differences between ViewModelProvider.Factory and ViewModelProvider.NewInstanceFactory? kotlin 中的lazy 和lazyFast 有什么区别? - what is the difference between lazy and lazyFast in kotlin? kotlin 中 lazy{} 与 getter() 初始化之间的区别 - Difference between lazy{} vs getter() initialization in kotlin 如何在活动之间共享 ViewModelProvider.Factory 实例 - How to share a ViewModelProvider.Factory instance between Activities 使用 activityViewModels 测试 android 片段 - Testing android fragments with activityViewModels ViewModelProvider.of 和 ViewModelProvider 在 Android Java 中均已弃用 - ViewModelProvider.of and ViewModelProvider both are deprecated in Android Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM