从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 get(@NonNull Class 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 get(@NonNull String key, @NonNull Class 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 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 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核心功能,配置更改时恢复数据。回到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 children = onRetainNonConfigurationChildInstances();
FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig();
mFragments.doLoaderStart();
mFragments.doLoaderStop(true);
ArrayMap 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(),取保存的ComponentActivity.NonConfigurationInstances实例。
public Object getLastNonConfigurationInstance() {
return mLastNonConfigurationInstances != null
? mLastNonConfigurationInstances.activity : null;
}
流程很清晰了:屏幕旋转配置更改时调用onRetainNonConfigurationInstance()保存了ViewModelStore,在Activity启动时调用的Activity.attach()中恢复。而ViewModelStore内部维护HashMap存储了ViewModel实例。