# 又到金三银四
往年面试中有关Jetpack的考察可以算是加分项,随着官方对Modern Android development (MAD) 的大力推广,今年基本上都是必选题了。
很多候选人对Jetpack各组件的功能及用法如数家珍,但一问及到原理往往卡壳。原理不清虽不影响API的使用,但也正因为如此,如果能对源码有一定了解,也许可以脱颖而出得到加分。
本文分享一个入门级的源码分析,也是在面试中经常被问到的问题
# ViewModel
ViewModel是Android Jetpack中的重要组件,其优势是具有下图这样的生命周期、不会因为屏幕旋转等Activity配置变化而销毁,是实现MVVM架构中UI状态管理的重要基础。
class MyActivity : AppCompatActivity {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.d(TAG, "onCreate")
val activity: FragmentActivity = this
val factory: ViewModelProvider.Factory = ViewModelProvider.NewInstanceFactory()
// Activity由于横竖品切换销毁重建,此处的viewModel 仍然是重建前的实例
val viewModel = ViewModelProvider(activity, factory).get(MyViewModel::class.java)
// 如果直接new实例则会创建新的ViewModel实例
// val viewModel = MyViewModel()
Log.d(TAG, " - Activity :${this.hashCode()}")
Log.d(TAG, " - ViewModel:${viewModel.hashCode()}")
}
}
复制代码
上面代码在横竖屏切换时的log如下:
#Activity初次启动
onCreate
- Activity :132818886
- ViewModel:249530701
onStart
onResume
#屏幕旋转
onPause
onStop
onRetainNonConfigurationInstance
onDestroy
onCreate
- Activity :103312713 #Activity实例不同
- ViewModel:249530701 #ViewModel实例相同
onStart
onResume
复制代码
下面代码是保证屏幕切换时ViewModel不销毁的关键,我们依次为入口看一下源码
val viewModel = ViewModelProvider(activity, factory).get(MyViewModel::class.java)
复制代码
# ViewModelProvider
ViewModelProvider源码很简单,分别持有一个ViewModelProvider.Factory
和ViewModelStore
实例
package androidx.lifecycle;
public class ViewModelProvider {
public interface Factory {
@NonNull
<T extends ViewModel> T create(@NonNull Class<T> modelClass);
}
private final Factory mFactory;
private final ViewModelStore mViewModelStore;
public ViewModelProvider(@NonNull ViewModelStoreOwner owner, @NonNull Factory factory) {
this(owner.getViewModelStore(), factory);
}
public ViewModelProvider(@NonNull ViewModelStore store, @NonNull Factory factory) {
mFactory = factory;
this.mViewModelStore = store;
}
...
}
复制代码
get()
返回ViewModel实例
package androidx.lifecycle;
public class ViewModelProvider {
...
public <T extends ViewModel> T get(@NonNull Class<T> modelClass) {
String canonicalName = modelClass.getCanonicalName();
...
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)) {
//noinspection unchecked
return (T) viewModel;
} else {
//noinspection StatementWithEmptyBody
if (viewModel != null) {
// TODO: log a warning.
}
}
viewModel = mFactory.create(modelClass);
mViewModelStore.put(key, viewModel);
//noinspection unchecked
return (T) viewModel;
}
...
}
复制代码
逻辑非常清晰:
- ViewModelProvider通过ViewModelStore获取ViewModel
- 若获取失败,则通过ViewModelProvider.Factory创建ViewModel
# ViewModelStore
package androidx.lifecycle;
public class ViewModelStore {
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);
}
public final void clear() {
for (ViewModel vm : mMap.values()) {
vm.onCleared();
}
mMap.clear();
}
}
复制代码
可见,ViewModelStore就是一个对Map
的封装。
val viewModel = ViewModelProvider(activity, factory).get(FooViewModel::class.java)
复制代码
上面代码ViewModelProvider()构造参数1中传入的FragmentActivity
(基类是ComponentActivity
)实际上是ViewModelStoreOwner
的一个实现。
package androidx.lifecycle;
public interface ViewModelStoreOwner {
@NonNull
ViewModelStore getViewModelStore();
}
复制代码
ViewModelProvider中的ViewModelStore正是来自ViewModelStoreOwner。
public class ViewModelProvider {
private final ViewModelStore mViewModelStore;
public ViewModelProvider(@NonNull ViewModelStoreOwner owner, @NonNull Factory factory) {
this(owner.getViewModelStore(), factory);
}
public ViewModelProvider(@NonNull ViewModelStore store, @NonNull Factory factory) {
this.mViewModelStore = store;
}
复制代码
Activity在onDestroy会尝试对ViewModelStore清空。如果是由于ConfigurationChanged带来的Destroy则不进行清空,避免横竖屏切换等造成ViewModel销毁。
//ComponentActivity.java
getLifecycle().addObserver(new LifecycleEventObserver() {
@Override
public void onStateChanged(@NonNull LifecycleOwner source,
@NonNull Lifecycle.Event event) {
if (event == Lifecycle.Event.ON_DESTROY) {
// Clear out the available context
mContextAwareHelper.clearAvailableContext();
// And clear the ViewModelStore
if (!getLifecycle().addObserver(new LifecycleEventObserver() {
@Override
public void onStateChanged(@NonNull LifecycleOwner source,
@NonNull Lifecycle.Event event) {
if (event == Lifecycle.Event.ON_DESTROY) {
// Clear out the available context
mContextAwareHelper.clearAvailableContext();
// And clear the ViewModelStore
if (!isChangingConfigurations()) {
getViewModelStore().clear();
}
}
}
});()) {
getViewModelStore().clear();
}
}
}
});
复制代码
# FragmentActivity#getViewModelStore()
FragmentActivity
实现了ViewModelStoreOwner
的getViewModelStore
方法
package androidx.fragment.app;
public class FragmentActivity extends ComponentActivity implements ViewModelStoreOwner ... {
private ViewModelStore mViewModelStore;
@NonNull
@Override
public ViewModelStore 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();
}
}
return mViewModelStore;
}
static final class NonConfigurationInstances {
Object custom;
ViewModelStore viewModelStore;
FragmentManagerNonConfig fragments;
}
...
}
复制代码
通过getLastNonConfigurationInstance()
获取 NonConfigurationInstances
实例,从而得到真正的viewModelStore
,getLastNonConfigurationInstance()
又是什么?
# Activity#getLastNonConfigurationInstance()
package android.app;
public class Activity extends ContextThemeWrapper implements ... {
/* package */ NonConfigurationInstances mLastNonConfigurationInstances;
@Nullable
public Object getLastNonConfigurationInstance() {
return mLastNonConfigurationInstances != null
? mLastNonConfigurationInstances.activity : null;
}
复制代码
Retrieve the non-configuration instance data that was previously returned by onRetainNonConfigurationInstance(). This will be available from the initial onCreate(Bundle) and onStart() calls to the new instance, allowing you to extract any useful dynamic state from the previous instance.
通过官方文档我们知道,屏幕旋转前通过onRetainNonConfigurationInstance()
返回的Activity实例,屏幕旋转后可以通过getLastNonConfigurationInstance()
获取,因此屏幕旋转前后不销毁的关键就在onRetainNonConfigurationInstance
# Activity#onRetainNonConfigurationInstance()
#Activity初次启动
onCreate
- Activity :132818886
- ViewModel:249530701
onStart
onResume
#屏幕旋转
onPause
onStop
onRetainNonConfigurationInstance
onDestroy
onCreate
- Activity :103312713 #Activity实例不同
- ViewModel:249530701 #ViewModel实例相同
onStart
onResume
复制代码
屏幕旋转时,onRetainNonConfigurationInstance()
在onStop
和onDestroy
之间调用
package android.app;
public class Activity extends ContextThemeWrapper implements ... {
public Object onRetainNonConfigurationInstance() {
return null;
}
...
}
复制代码
onRetainNonConfigurationInstance
在Activity中只有空实现,在FragmentActivity
中被重写
package androidx.fragment.app;
public class FragmentActivity extends ComponentActivity implements ViewModelStoreOwner, ... {
@Override
public final Object onRetainNonConfigurationInstance() {
Object custom = onRetainCustomNonConfigurationInstance();
FragmentManagerNonConfig fragments = mFragments.retainNestedNonConfig();
if (fragments == null && mViewModelStore == null && custom == null) {
return null;
}
NonConfigurationInstances nci = new NonConfigurationInstances();
nci.custom = custom;
nci.viewModelStore = mViewModelStore;
nci.fragments = fragments;
return nci;
}
static final class NonConfigurationInstances {
Object custom;
ViewModelStore viewModelStore;
FragmentManagerNonConfig fragments;
}
...
}
复制代码
FragmentActivity 通过 onRetainNonConfigurationInstance() 返回 了存放ViewModelStore的NonConfigurationInstances 实例。 值得一提的是onRetainNonConfigurationInstance
提供了一个hook时机:onRetainCustomNonConfigurationInstance
,允许我们像ViewModel一样使得自定义对象不被销毁
NonConfigurationInstances会在attach
中由系统传递给新重建的Activity:
final void attach(Context context, ActivityThread aThread,
Instrumentation instr, IBinder token, int ident,
Application application, Intent intent, ActivityInfo info,
CharSequence title, Activity parent, String id,
NonConfigurationInstances lastNonConfigurationInstances,
Configuration config, String referrer, IVoiceInteractor voiceInteractor,
Window window, ActivityConfigCallback activityConfigCallback, IBinder assistToken)
复制代码
然后在onCreate
中,通过getLastNonConfigurationInstance()
获取NonConfigurationInstances中的ViewModelStore
package androidx.fragment.app;
public class FragmentActivity extends ComponentActivity implements ViewModelStoreOwner ... {
private ViewModelStore mViewModelStore;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
mFragments.attachHost(null /*parent*/);
super.onCreate(savedInstanceState);
NonConfigurationInstances nc =
(NonConfigurationInstances) getLastNonConfigurationInstance();
if (nc != null && nc.viewModelStore != null && mViewModelStore == null) {
mViewModelStore = nc.viewModelStore;
}
...
}
}
复制代码
# 总结
Activity首次启动
- FragmentActivity#onCreate()被调用
- 此时 FragmentActivity 的 mViewModelStore 尚为 null
- HogeActivity的onCreate() 被调用
- ViewModelProvider 实例创建
- FragmentActivity#getViewModelStore() 被调用,mViewModelStore被创建并赋值
发生屏幕旋转
- FragmentActivity#onRetainNonConfigurationInstance() 被调用
- 持有mViewModelStore 的NonConfigurationInstances 实例被返回
Activity重建
- FragmentActivity#onCreate() 被调用
- 从Activity#getLastNonConfigurationInstance() 获取 NonConfigurationInstances 实例
- NonConfigurationInstances 中保存了屏幕旋转前的 FragmentActivity 的 mViewModelStore,将其赋值给重建后的FragmentActivity 的 mViewModelStore
- HogeActivity#onCreate() 被调用
- 通过ViewModelProvider#get() 获取 ViewModel 实例
本文在开源项目:https://github.com/Android-Alvin/Android-LearningNotes 中已收录,里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…