栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android - 带着问题看源码之 ViewModel

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android - 带着问题看源码之 ViewModel

// 注入数据参数

fun provideUserViewModelFactory(): MyViewModelFactory {

val repository = UserRepository.getInstance()

return MyViewModelFactory(repository)

}

}

// ViewModel 工厂类

class MyViewModelFactory(

private val userRepository: UserRepository,

) : ViewModelProvider.Factory {

@Suppress(“UNCHECKED_CAST”)

override fun create(modelClass: Class): T {

return MyViewModel(userRepository) as T

}

}

// ViewModel

class MyViewModel(private val userRepository: UserRepository ):ViewModel() {

private var userInfo: MutableLiveData? = null

fun getUserInfo(): LiveData? {

if (userInfo == null) {

userInfo = MutableLiveData()

loadUserInfo()

}

return userInfo

}

private fun loadUserInfo() {

this.userInfo = userRepository.getUserInfo()

}

}

// User 仓库

class UserRepository {

fun getUserInfo():MutableLiveData{

val user = MutableLiveData()

user.value = User(“张三”,“18”)

return user

}

companion object {

// For Singleton instantiation

@Volatile

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 接下来我们带着开头的几个问题,深入源码看看它是如何实现的。

[](()(1) 生命周期比组件的长如何实现


上图是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

那么我们去源码中找找看有没有什么蛛丝马迹

[](()(2) 页面配置修改后如何保存数据


源码: 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);

}

@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;

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/845835.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号