[英]What is ViewModelStore and viewModelStoreOwner?
I am very confused due to this new ViewModelProvider api(ViewModelProviders is deprecated)由于这个新的 ViewModelProvider api,我很困惑(不推荐使用 ViewModelProviders)
As with the new changes there are new Constructors also (Source code).与新的变化一样,也有新的构造函数(源代码)。
public ViewModelProvider(@NonNull ViewModelStoreOwner owner) {
this(owner.getViewModelStore(), owner instanceof HasDefaultViewModelProviderFactory
? ((HasDefaultViewModelProviderFactory) owner).getDefaultViewModelProviderFactory()
: NewInstanceFactory.getInstance());
}
public ViewModelProvider(@NonNull ViewModelStoreOwner owner, @NonNull Factory factory) {
this(owner.getViewModelStore(), factory);
}
public ViewModelProvider(@NonNull ViewModelStore store, @NonNull Factory factory) {
mFactory = factory;
mViewModelStore = store;
}
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0-rc02"
So These Constructor's require ViewModelStore
and viewModelStoreOwner
.所以这些构造函数需要ViewModelStore
和viewModelStoreOwner
。
@param store {@code ViewModelStore} where ViewModels will be stored. @param store {@code ViewModelStore} 将存储 ViewModel。
@param owner a {@code ViewModelStoreOwner} whose {@link ViewModelStore} will be used to retain {@code ViewModels} @param owner 一个 {@code ViewModelStoreOwner},其 {@link ViewModelStore} 将用于保留 {@code ViewModels}
Can anyone define them and how to use them and what they really mean to us developer's?任何人都可以定义它们以及如何使用它们以及它们对我们开发人员的真正意义吗?
is ViewModelStoreOwner==activity/fragment?是 ViewModelStoreOwner==活动/片段?
Can anyone define them and how to use them and what they really mean to us developer's?任何人都可以定义它们以及如何使用它们以及它们对我们开发人员的真正意义吗?
A ViewModelStore
can be considered as a container that stores the ViewModels in a HashMap
.可以将ViewModelStore
视为将 ViewModel 存储在HashMap
中的容器。 Where the key
is string value and value is the ViewModel being saved( ViewModelProvider
uses a concatenation of the string_key
+ ViewModel class canonical name).其中key
是字符串值,值是要保存的 ViewModel( ViewModelProvider
使用string_key
+ ViewModel class 规范名称的串联)。
A ViewModelStoreOwner
is merely an interface. ViewModelStoreOwner
只是一个接口。 Any class that implements the getViewModelStore()
defined by this interface becomes the owner of ViewModelStore
.任何实现此接口定义的getViewModelStore()
的 class 都将成为ViewModelStore
的所有者。 This class then maintains the ViewModelStore
and should be responsible to appropriately restoring it when needed.这个 class 然后维护ViewModelStore
并且应该负责在需要时适当地恢复它。
We can implement our own version of the owner and the state based on the requirement.我们可以根据需要实现我们自己的所有者版本和 state。
is ViewModelStoreOwner==activity/fragment?是 ViewModelStoreOwner==活动/片段?
Yes.是的。 Based on the Android source code, both Fragment
(from androidx.fragment.app
) & ComponentActivity
(from androidx.activity
) implements ViewModelStoreOwner
.基于 Android 源代码, Fragment
(来自androidx.fragment.app
)和ComponentActivity
(来自androidx.activity
)都实现了ViewModelStoreOwner
。 These classes maintains a viewModelStore
and value is restored appropriately.这些类维护一个viewModelStore
并且值被适当地恢复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.