private var instance: UserRepository? = null
fun getInstance() =
instance ?: synchronized(this) {
instance ?: UserRepository().also { instance = it }
}
}
}
// User 实体
data class User(val name:String,val age:String)
以上示例实现了数据和 UI 分离,并且ViewModel
中没有持有View
接下来我们带着开头的几个问题,深入源码看看它是如何实现的。
上图是Activity
和 ViewModel
生命周期对比图,从图中可看出 ViewModel
的生命周期确实比Activity
长。那么 ViewModel
它是如何实现的呢?
其实主要用到了Jetpack 中的 LifeCycle
库,当然不用该库也是可以的,下篇文章我们再分析该库的实现。
首先我们要明确知道 ViewModel
的作用之一就是是:通过关联生命周期的方式来存储和管理跟UI相关的数据 而LifeCycle
库是专门用来处理生命周期的库,也就是该库可以感知Activity
的生命周期,并在不同的生命周期处理相关的逻辑。
上图也看出了 ViewModel
的生命周期比Activity
的onDestory
生命周期还长并且多了个方法onCleared
。 既然是在 Activity 的 onDestory 生命周期之后,那我们跟进源码看看它是怎么处理的。
源码: Android API 29 查看顺序:AppCompatActivity—>FragmentActivity—>ComponentActivity
AppCompatActivity.java 中只是委托了事件,具体的处理逻辑要在不同的 Android API 版本中处理,这就不在本文的介绍范围了,可搜索 Activity 的加载流程详细了解
@Override
protected void onDestroy() {
super.onDestroy();
getDelegate().onDestroy();
}
FragmentActivity.java 中处理了 ON_DESTROY 事件
@Override
protected void onDestroy() {
super.onDestroy();
mFragments.dispatchDestroy();
mFragmentLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
}
ComponentActivity.java 的构造函数里面观察了 Lifecycle.Event.ON_DESTROY 事件,并获取 ViewModelStore 调用 clear 方法清除数据
public ComponentActivity() {
Lifecycle lifecycle = getLifecycle();
…
getLifecycle().addObserver(new LifecycleEventObserver() {
@Override
public void onStateChanged(@NonNull LifecycleOwner source,
@NonNull Lifecycle.Event event) {
if (event == Lifecycle.Event.ON_DESTROY) {
// isChangingConfigurations 方法是检查配置文件是否修改,如果修改则返回true,没有修改则返回false
// 这里取反,就是保证了在配置文件修改的时候不会清除数据
if (!isChangingConfigurations()) {
// 清除数据
getViewModelStore().clear();
}
}
}
});
}
从上面的源码分析也看出了ViewModel
确实是在Activity
的生命周期onDestory
之后监听到Lifecycle.Event.ON_DESTROY
再去解决ViewModel
中的数据清除问题。 而源码中也做了判断,在页面配置文件修改后并不会清除数据。这也进一步说明了ViewModel
能解决因页面配置文件修改后清除数据的问题。
具体如何保存和如何恢复数据,我们知道在配置文件后页面会销毁和重建,这个过程中会使用到下面两个方法。
-
onSaveInstanceState
-
onRestoreInstanceState
那么我们去源码中找找看有没有什么蛛丝马迹
源码: Android API 29 查看顺序:AppCompatActivity—>FragmentActivity—>ComponentActivity
AppCompatActivity.java 同样的这里也只是委托了事件
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
getDelegate().onSaveInstanceState(outState);
}
FragmentActivity.java 中处理了数据的保存
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
markFragmentsCreated();
mFragmentLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
Parcelable p = mFragments.saveAllState();
if (p != null) {
outState.putParcelable(FRAGMENTS_TAG, p);
}
if (mPendingFragmentActivityResults.size() > 0) {
outState.putInt(NEXT_CANDIDATE_REQUEST_INDEX_TAG, mNextCandidateRequestIndex);
int[] requestCodes = new int[mPendingFragmentActivityResults.size()];
String[] fragmentWhos = new String[mPendingFragmentActivityResults.size()];
for (int i = 0; i < mPendingFragmentActivityResults.size(); i++) {
requestCodes[i] = mPendingFragmentActivityResults.keyAt(i);
fragmentWhos[i] = mPendingFragmentActivityResults.valueAt(i);
}
outState.putIntArray(ALLOCATED_REQUEST_INDICIES_TAG, requestCodes);
outState.putStringArray(REQUEST_FRAGMENT_WHO_TAG, fragmentWhos);
}
}
ComponentActivity.java 的 onSaveInstanceState 方法中还是没有 ViewModel 的身影,但是紧接着的方法 onRetainNonConfigurationInstance 的注释有点意思,它是 final 所以不能重写,但是它里面又有一个公开的 onRetainCustomNonConfigurationInstance 方法,所以说我们可以重写该方法返回一些 Object 的对象,但是该方法过时了。
推荐我们使用 ViewModel 来实现。
@CallSuper
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
Lifecycle lifecycle = getLifecycle();
if (lifecycle instanceof LifecycleRegistry) {
((LifecycleRegistry) lifecycle).setCurrentState(Lifecycle.State.CREATED);
}
super.onSaveInstanceState(outState);
mSavedStateRegistryController.performSave(outState);
}
/**
-
Retain all appropriate non-config state. You can NOT
-
override this yourself! Use a {@link androidx.lifecycle.ViewModel} if you want to
-
retain your own non config state.
*/
@Override
@Nullable
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
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;
}
所以我们还是回到 ComponentActivity.java 的构造方法中找到 Lifecycle.Event.ON_DESTROY 的处理,
在里面有个方法 getViewModelStore 获取到了 ViewModel 的数据,它的实现如下:
@NonNull
@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;
}
如果不使用 ViewModel
,在页面销毁和重建时可以用重写下面两个方法保存和恢复数据
-
onSaveInstanceState
-
onRestoreInstanceState
但是我们知道这两个方法只能保存 Bundle 支持的数据,要想保存其它数据就得用到下面的方法了
-
onRetainNonConfigurationInstance:final 所以不能重写,作用是保存数据,调用时机是
onStop()
和onDestroy()
之间 -
onRetainCustomNonConfigurationInstance:@Deprecated 过时了,推荐使用 ViewModel
-
getLastNonConfigurationInstance:作用是取出数据,调用时机是
onCreate()
之后 -
getLastCustomNonConfigurationInstance:@Deprecated 过时了,推荐使用 ViewModel
如果使用ViewModel
,在页面销毁和重建时就不需要你操心数据的保存和恢了。
ViewModel
会在onRetainNonConfigurationInstance
方法中保存数据,如下:
ViewModelStore viewModelStore = mViewModelStore;
if (viewModelStore == null) {
// No one called getViewModelStore(), so see if there was an existing
// ViewModelStore from our last NonConfigurationInstance
NonConfigurationInstances nc =
(NonConfigurationInstances) getLastNonConfigurationInstance();
if (nc != null) {
viewModelStore = nc.viewModelStore;
}
}
如果Activity
重写了onRetainCustomNonConfigurationInstance
方法还可以将自定义的Object
类型的对象保存到NonConfigurationInstances 的 custom
属性中。如下:
NonConfigurationInstances nci = new NonConfigurationInstances();
nci.custom = custom;
nci.viewModelStore = viewModelStore;
并且还会在Activity
的onDestory
生命周期判断页面是否重建,重建则不删除ViewModel
中的数据
源码: Android API 29
首先我们要知道内存为何会泄漏。
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
最后
跳槽季整理面试题已经成了我多年的习惯!在这里我和身边一些朋友特意整理了一份快速进阶为Android高级工程师的系统且全面的学习资料。涵盖了Android初级——Android高级架构师进阶必备的一些学习技能。
附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题(含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)
的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!**
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
[外链图片转存中…(img-a1ZGKirD-1712062761979)]
最后
跳槽季整理面试题已经成了我多年的习惯!在这里我和身边一些朋友特意整理了一份快速进阶为Android高级工程师的系统且全面的学习资料。涵盖了Android初级——Android高级架构师进阶必备的一些学习技能。
附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题(含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)
[外链图片转存中…(img-gJJB5UtX-1712062761980)]