Android ViewModel原理分析
从API调用入手
创建ViewModel实例
//MainActivity
ViewModelProvider(this)[MainViewModel::class.java]
this--->MainActivity
Activity继承关系:
MainActivity->AppCompatActivity->FragmentActivity->ComponentActivity
ComponentActivity实现ViewModelStoreOwner接口重写getViewModelStore()方法
@Override
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) {
mViewModelStore = new ViewModelStore();
}
}
return mViewModelStore;
}
ViewModelProvider(this)
public ViewModelProvider(@NonNull ViewModelStoreOwner owner) {
this(owner.getViewModelStore(), owner instanceof HasDefaultViewModelProviderFactory
? ((HasDefaultViewModelProviderFactory) owner).getDefaultViewModelProviderFactory()
: NewInstanceFactory.getInstance());
}
public ViewModelProvider(@NonNull ViewModelStore store, @NonNull Factory factory) {
mFactory = factory;
mViewModelStore = store;
}
ViewModelStoreOwner--->ComponentActivity
owner.getViewModelStore()--->ViewModelStore
ViewModelProvider.get()
public <T extends ViewModel> T get(@NonNull Class<T> modelClass) {
String canonicalName = modelClass.getCanonicalName();
if (canonicalName == null) {
throw new IllegalArgumentException("Local and anonymous classes can not be ViewModels");
}
return get(DEFAULT_KEY + ":" + canonicalName, modelClass);
}
public <T extends ViewModel> T get(@NonNull String key, @NonNull Class<T> modelClass) {
ViewModel viewModel = mViewModelStore.get(key);
if (modelClass.isInstance(viewModel)) {
if (mFactory instanceof OnRequeryFactory) {
((OnRequeryFactory) mFactory).onRequery(viewModel);
}
return (T) viewModel;
} else {
//noinspection StatementWithEmptyBody
if (viewModel != null) {
// TODO: log a warning.
}
}
if (mFactory instanceof KeyedFactory) {
viewModel = ((KeyedFactory) (mFactory)).create(key, modelClass);
} else {
viewModel = (mFactory).create(modelClass);
}
mViewModelStore.put(key, viewModel);
return (T) viewModel;
}
ViewModelStore.get()
private final HashMap<String, ViewModel> mMap = new HashMap<>();
final void put(String key, ViewModel viewModel) {
ViewModel oldViewModel = mMap.put(key, viewModel);
if (oldViewModel != null) {
oldViewModel.onCleared();
}
}
final ViewModel get(String key) {
return mMap.get(key);
}
Set<String> keys() {
return new HashSet<>(mMap.keySet());
}
public final void clear() {
for (ViewModel vm : mMap.values()) {
vm.clear();
}
mMap.clear();
}
API调用流程总结:
ViewModelProvider(this)传入ViewModelStoreOwner接口实现类ComponentActivity,ComponentActivity重写了抽象方法getViewModelStore()返回ViewModelStore;
ViewModelProvider.get(ViewModel::class.java)通过ViewModelStore获取ViewModel;
ViewModelStore内部维护了一个HashMap,key是ViewModel类名,value是ViewModel实例。
接下来看ViewModel核心功能,Activity重建恢复数据。
回到ComponentActivity.getViewModelStore()
if (mViewModelStore == null) {
NonConfigurationInstances nc =
(NonConfigurationInstances) getLastNonConfigurationInstance();
if (nc != null) {
// Restore the ViewModelStore from NonConfigurationInstances
mViewModelStore = nc.viewModelStore;
}
if (mViewModelStore == null) {
mViewModelStore = new ViewModelStore();
}
}
nc不为null,通过nc获取viewModelStore
获取nc--->getLastNonConfigurationInstance()
调用父类Activity.getLastNonConfigurationInstance()
public Object getLastNonConfigurationInstance() {
return mLastNonConfigurationInstances != null
? mLastNonConfigurationInstances.activity : null;
}
找一下mLastNonConfigurationInstances赋值处
Activity.attach()
...
mLastNonConfigurationInstances = lastNonConfigurationInstances;
...
至于attach()方法调用处,前面很多篇都说过
ActivityThread.performLaunchActivity(ActivityClientRecord r...)
activity.attach(appContext, this, getInstrumentation(), r.token,
r.ident, app, r.intent, r.activityInfo, title, r.parent,
r.embeddedID, r.lastNonConfigurationInstances, config,
r.referrer, r.voiceInteractor, window, r.configCallback,
r.assistToken);
lastNonConfigurationInstances从ActivityClientRecord中获取
lastNonConfigurationInstances何时保存到ActivityClientRecord中的呢?
既然ViewModel在屏幕旋转,Activity销毁重建后可恢复数据,先找到Activity.Destroy()调用处
ActivityThread.performDestroyActivity()
ActivityClientRecord r = mActivities.get(token);
r.lastNonConfigurationInstances = r.activity.retainNonConfigurationInstances();
Activity.retainNonConfigurationInstances()
NonConfigurationInstances retainNonConfigurationInstances() {
Object activity = onRetainNonConfigurationInstance();
HashMap<String, Object> children = onRetainNonConfigurationChildInstances();
FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig();
mFragments.doLoaderStart();
mFragments.doLoaderStop(true);
ArrayMap<String, LoaderManager> loaders = mFragments.retainLoaderNonConfig();
if (activity == null && children == null && fragments == null && loaders == null
&& mVoiceInteractor == null) {
return null;
}
NonConfigurationInstances nci = new NonConfigurationInstances();
nci.activity = activity;
nci.children = children;
nci.fragments = fragments;
nci.loaders = loaders;
if (mVoiceInteractor != null) {
mVoiceInteractor.retainInstance();
nci.voiceInteractor = mVoiceInteractor;
}
return nci;
}
Object activity = onRetainNonConfigurationInstance()
调用子类ComponentActivity.onRetainNonConfigurationInstance()
public final Object onRetainNonConfigurationInstance() {
Object custom = onRetainCustomNonConfigurationInstance();
ViewModelStore viewModelStore = mViewModelStore;
if (viewModelStore == null) {
NonConfigurationInstances nc =
(NonConfigurationInstances) getLastNonConfigurationInstance();
if (nc != null) {
viewModelStore = nc.viewModelStore;
}
}
if (viewModelStore == null && custom == null) {
return null;
}
NonConfigurationInstances nci = new NonConfigurationInstances();
nci.custom = custom;
nci.viewModelStore = viewModelStore;
return nci;
}
static final class NonConfigurationInstances {
Object custom;
ViewModelStore viewModelStore;
}
nci.activity = activity
这里activity其实是个Object对象也就是上述方法返回值:
ComponentActivity.NonConfigurationInstances
其内部保存ViewModelStore实例
回看Activity.getLastNonConfigurationInstance()
public Object getLastNonConfigurationInstance() {
return mLastNonConfigurationInstances != null
? mLastNonConfigurationInstances.activity : null;
}
取保存的ComponentActivity.NonConfigurationInstances实例
流程很清晰了:
Activity.Destroy()时保存了ViewModelStore,在Activity启动、实例化时调用Activity.attach()恢复。ViewModelStore内部维护HashMap存储ViewModel实例。