androidx ViewModel 生命周期如何与Activity/Fragment生命周期关联

通常我们在Activity/Fragment中创建ViewModel使用如下方式:

NameViewModel nameViewModel=new ViewModelProvider(this).get(NameViewModel.class);

其中ViewModelStore对象由Activity/Fragment提供,并且在Activity/Fragment销毁时调用ViewModel的clear方法

那Activity、Fragment如何创建ViewModelStore的呢?让我我们一起来看看源码,解开疑惑

与Activity关联的ViewModelStore对象

ViewModelStore 用于存储ViewModel实例对象,在androidx.activity.ComponentActivity中实现了ViewModelStoreOwner接口,
实现代码如下:

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

第13行获取上一次的对象,Android系统提供的一种机制,在系统配置(如屏幕有竖屏变为横屏时)改变时,Activity将会重新创建,通过覆盖onRetainNonConfigurationInstance,将viewModelStore返回,确保ViewModelStore不会从新创建,以此保证与该ViewModelStore关联的ViewModel不因配置改变而丢失,onRetainNonConfigurationInstance代码如下

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

从上面的源码分析可知,只要利用相同的Activity实例调用ViewModelProvider(thisAcitivty).get(viewModelClass) 创建的将会是同一个对象,在该Activity关联的Fragment中也一样。

与androidx.fragment.app.Fragment关联的ViewModelStore对象

同样androidx.fragment.app.Fragment也实现了ViewModelStoreOwner接口
源码如下:

    public ViewModelStore getViewModelStore() {
        if (mFragmentManager == null) {
            throw new IllegalStateException("Can't access ViewModels from detached fragment");
        }
        return mFragmentManager.getViewModelStore(this);
    }

可以看出ViewModelStore的获取委托给了FragmentManager实现

继续看 FragmentManager getViewModelStore 的方法

    
    private FragmentManagerViewModel mNonConfig;
    
    ....省略无关代码

    ViewModelStore getViewModelStore(@NonNull Fragment f) {
        return mNonConfig.getViewModelStore(f);
    }

在FragmentManager中通过mNonConfig获取ViewModelStore对象,看看mNonConfig如果创建的:

void attachController(@NonNull FragmentHostCallback<?> host,
            @NonNull FragmentContainer container, @Nullable final Fragment parent) {
        if (mHost != null) throw new IllegalStateException("Already attached");
        mHost = host;
        mContainer = container;
        mParent = parent;
        ...省略无关代码

        // Get the FragmentManagerViewModel
        if (parent != null) {
            mNonConfig = parent.mFragmentManager.getChildNonConfig(parent);
        } else if (host instanceof ViewModelStoreOwner) {
            ViewModelStore viewModelStore = ((ViewModelStoreOwner) host).getViewModelStore();
            mNonConfig = FragmentManagerViewModel.getInstance(viewModelStore);
        } else {
            mNonConfig = new FragmentManagerViewModel(false);
        }
    }

这里在创建mNonConfig时有三种情形:

第一种:当parent!=null说明是在Fragment中又启动的Fragment,调用父Fragment的FragmentManager.getChildNonConfig()创建FragmentManagerViewModel,实际是到查找父Fragment关联的FragmentManagerViewModel 的HashMap中是否有子Fragment的FragmentManagerViewModel,没有就创建,并缓存到mChildNonConfigs中

第二种:当host实现了ViewModelStoreOwner接口,这中情况大多数是在Activity中启动了Fragment,host就是Activity,host.getViewModelStore()获取的即是Activity的ViewModelStore。通过FragmentManagerViewModel.getInstance(viewModelStore)创建FragmentManagerViewModel,实际是利用Activity的ViewModelProvider创建,通过分析我们能构发现,创建的FragmentManagerViewModel也在Activity的ViewModelStore中。这样在Activity/Fragment销毁时FragmentManagerViewModel 会跟着Activity/fragment的销毁

第三中:如果非以上两种就直接创建实例并且不保存FragmentManagerViewModel的状态,通常不会到这个分支

FragmentManagerViewModel 数据结构

final class FragmentManagerViewModel extends ViewModel {
    
    private static final ViewModelProvider.Factory FACTORY = new ViewModelProvider.Factory() {
        @NonNull
        @Override
        @SuppressWarnings("unchecked")
        public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
            FragmentManagerViewModel viewModel = new FragmentManagerViewModel(true);
            return (T) viewModel;
        }
    };

    @NonNull
    static FragmentManagerViewModel getInstance(ViewModelStore viewModelStore) {
        ViewModelProvider viewModelProvider = new ViewModelProvider(viewModelStore,
                FACTORY);
        return viewModelProvider.get(FragmentManagerViewModel.class);
    }

    ...省略无关代码
    private final HashMap<String, Fragment> mRetainedFragments = new HashMap<>();
    private final HashMap<String, FragmentManagerViewModel> mChildNonConfigs = new HashMap<>();
    private final HashMap<String, ViewModelStore> mViewModelStores = new HashMap<>();

    ...省略无关代码

    @NonNull
    FragmentManagerViewModel getChildNonConfig(@NonNull Fragment f) {
        FragmentManagerViewModel childNonConfig = mChildNonConfigs.get(f.mWho);
        if (childNonConfig == null) {
            childNonConfig = new FragmentManagerViewModel(mStateAutomaticallySaved);
            mChildNonConfigs.put(f.mWho, childNonConfig);
        }
        return childNonConfig;
    }

    @NonNull
    ViewModelStore getViewModelStore(@NonNull Fragment f) {
        ViewModelStore viewModelStore = mViewModelStores.get(f.mWho);
        if (viewModelStore == null) {
            viewModelStore = new ViewModelStore();
            mViewModelStores.put(f.mWho, viewModelStore);
        }
        return viewModelStore;
    }
}


总结

  • ViewModel通过Activity/Fragment提供的ViewModelStore将ViewModel的生命周期跟Activity/Fragment关联起来(ViewModel 的生命周期非常简单就是onCleared方法)。
  • ViewModel具有生命范围,Activity范围和Fragment范围
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ViewModel生命周期Activity生命周期是不同的。在图1中,我们可以看到Activity经历了屏幕旋转并结束的过程中,它的生命周期状态会发生变化。而ViewModel生命周期Activity生命周期无关,它会在Activity被销毁后继续存在。 ViewModel的主要目的是存储和管理与UI相关的数据,并且在Activity被销毁后重新创建时能够保留这些数据。ViewModel生命周期会持续到与其关联Activity被彻底销毁。这意味着当Activity被重新创建时,ViewModel仍然存在,并且可以继续提供数据给新创建的Activity使用。 因此,ViewModel生命周期相对于Activity来说更长久,并且能够在Activity的销毁和重新创建过程中保持数据的一致性。这使得在Fragment之间共享数据变得更加方便,因为多个Fragment可以与同一个ViewModel进行通信,而不受Activity生命周期的影响。 总结来说,ViewModel生命周期Activity生命周期无关,它可以在Activity被销毁后继续存在,并且能够保持数据的一致性,从而方便在Fragment之间共享数据。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Android Jetpack架构组件-ViewModel的使用及原理](https://blog.csdn.net/qq_15988951/article/details/105106867)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [android-activity-lifecycle:向任何类提供一种侦听android活动生命周期事件的方法](https://download.csdn.net/download/weixin_42128315/18327364)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值