前言
ViewModel可以感知activity的生命周期来管理UI相关的数据,在屏幕旋转后数据仍然存在。在传统的UI控制器中,譬如activity或fragment屏幕发生转动时,可以使用onSaveInstanceState()方法并从onCreate()中的包中恢复其数据,但此方法仅适用于可以序列化然后反序列化的少量数据。还有一个就是资源对象使用完成时,对资源对象的回收以避免内存泄漏。而且,在配置更改时,会造成资源对象的重建不仅浪费资源,还需要额外的管理。此外,UI控制器还要负责网络请求,太过臃肿。
创建viewModel的创建
ViewModelProviders.of(this).get(viewModelClass)
去看看of里面做了啥事
public static ViewModelProvider of(@NonNull FragmentActivity activity) {
return of(activity, null);
}
public static ViewModelProvider of(@NonNull FragmentActivity activity,
@Nullable Factory factory) {
Application application = checkApplication(activity);
if (factory == null) {
注释① //创建一个工厂
factory = ViewModelProvider.AndroidViewModelFactory.getInstance(application);
}
return new ViewModelProvider(activity.getViewModelStore(), factory);
}
Factory是个接口,里面就一个create()方法,有两个实现类,一个是NewInstanceFactory,AndroidViewModelFactory ,区别是第二个是带有application的,第一个是不带参数的。这里就看看第二个。
public static class AndroidViewModelFactory extends ViewModelProvider.NewInstanceFactory {
private static AndroidViewModelFactory sInstance;
@NonNull
public static AndroidViewModelFactory getInstance(@NonNull Application application) {
if (sInstance == null) {
sInstance = new AndroidViewModelFactory(application);
}
return sInstance;
}
private Application mApplication;
public AndroidViewModelFactory(@NonNull Application application) {
mApplication = application;
}
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
if (AndroidViewModel.class.isAssignableFrom(modelClass)) {
//通过反射去创建ViewModel的实例。
try {
return modelClass.getConstructor(Application.class).newInstance(mApplication);
}
return super.create(modelClass);
}
注释①:这个create目前还没掉,还没生成想要的viewModel。
接着往下看new ViewModelProvider(activity.getViewModelStore(), factory);把创建好的工厂传递出去
先看看第一个形参activity.getViewModelStore()
public ViewModelStore getViewModelStore() {
if (getApplication() == null) {
throw new IllegalStateException("Your activity is not yet attached to the "
+ "Application instance. You can't request ViewModel before onCreate call.");
}
if (mViewModelStore == null) {
//获取最近一次横竖屏切换时保存下来的数据
NonConfigurationInstances nc =
(NonConfigurationInstances) getLastNonConfigurationInstance();
if (nc != null) {
// Restore the ViewModelStore from NonConfigurationInstances
mViewModelStore = nc.viewModelStore;
}
if (mViewModelStore == null) {
//这就是一个存放ViewMode的容器
mViewModelStore = new ViewModelStore();
}
}
return mViewModelStore;
}
static final class NonConfigurationInstances {
Object custom;
//ViewModel容器
ViewModelStore viewModelStore;
}
前面提到,横竖屏切换时,使用viewModel不会影响到数据的变化,接下来揭开它神秘的面纱。
public final Object onRetainNonConfigurationInstance() {
Object custom = onRetainCustomNonConfigurationInstance();
ViewModelStore viewModelStore = mViewModelStore;
if (viewModelStore == null) {
// No one called getViewModelStore(), so see if there was an existing
// ViewModelStore from our last NonConfigurationInstance
//ViewModel保存到NonConfigurationInstances 中
NonConfigurationInstances nc =
(NonConfigurationInstances) getLastNonConfigurationInstance();
if (nc != null) {
//去取viewModelStore
viewModelStore = nc.viewModelStore;
}
}
if (viewModelStore == null && custom == null) {
return null;
}
//这里开始创建,nci和nc都是NonConfigurationInstances
NonConfigurationInstances nci = new NonConfigurationInstances();
nci.custom = custom;
nci.viewModelStore = viewModelStore;
return nci;
}
public Object getLastCustomNonConfigurationInstance() {
NonConfigurationInstances nc = (NonConfigurationInstances)
getLastNonConfigurationInstance();
return nc != null ? nc.custom : null;
}
可以看出,这里将ViewModel保存到了NonConfigurationInstances 中,在横竖屏切换时,还是用的原来的ViewMode,所以数据不会发生丢失。
NonConfigurationInstances mLastNonConfigurationInstances
protected void onCreate(@Nullable Bundle savedInstanceState) {
if (DEBUG_LIFECYCLE) Slog.v(TAG, "onCreate " + this + ": " + savedInstanceState);
if (mLastNonConfigurationInstances != null) {
//这里开始取
mFragments.restoreLoaderNonConfig(mLastNonConfigurationInstances.loaders);
}
}
关于资源回收
在ComponentActivity中对viewModel清空。
if (event == Lifecycle.Event.ON_DESTROY) {
if (!isChangingConfigurations()) {
getViewModelStore().clear();
}
}
网友评论