由于这个新的ViewModelProvider api,我非常困惑(不推荐使用ViewModelProviders)
与新更改一样,也有新的构造函数(源代码)。
#1
public ViewModelProvider(@NonNull ViewModelStoreOwner owner) {
this(owner.getViewModelStore(), owner instanceof HasDefaultViewModelProviderFactory
? ((HasDefaultViewModelProviderFactory) owner).getDefaultViewModelProviderFactory()
: NewInstanceFactory.getInstance());
}
#2
public ViewModelProvider(@NonNull ViewModelStoreOwner owner, @NonNull Factory factory) {
this(owner.getViewModelStore(), factory);
}
#3
public ViewModelProvider(@NonNull ViewModelStore store, @NonNull Factory factory) {
mFactory = factory;
mViewModelStore = store;
}
摇篮依赖度:
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0-rc02"
所以这些构造函数的要求 ViewModelStore
和viewModelStoreOwner
。
文件:
@param store {@code ViewModelStore}将在其中存储ViewModels。
@param拥有者{@code ViewModelStoreOwner},其{@link ViewModelStore}将用于保留{@code ViewModels}
谁能定义它们以及如何使用它们以及它们对我们开发人员的真正意义是什么?
是ViewModelStoreOwner ==活动/片段吗?