草庐IT

Android MVVM学习之----ViewModel、ViewModelProvider、ComponentActivity的关系

mldxs 2023-04-03 原文

目录

一、ViewModel的出生

二、ViewModel的结束

三、总结:


我们通过ViewModel的生命周期来作为主线,讲解ViewModel、ViewModelProvider、ComponentActivity三者的关系。

一、ViewModel的出生

创建ViewModel,通过调用ViewModelProvider的get方法。

mainViewModel = ViewModelProvider(this).get(MainViewModel::class.java)
//ViewModelProvider.class
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;
}
public ViewModelStore getViewModelStore() {
    ...
    ensureViewModelStore();
    return mViewModelStore;
}
void ensureViewModelStore() {
    if (mViewModelStore == null) {
        NonConfigurationInstances nc =
                (NonConfigurationInstances) getLastNonConfigurationInstance();
        if (nc != null) {
            // Restore the ViewModelStore from NonConfigurationInstances
            mViewModelStore = nc.viewModelStore;
        }
        if (mViewModelStore == null) {
            mViewModelStore = new ViewModelStore();
        }
    }
}

获取owner的ViewModelStore(AppCompatActivity 兼容 -> FragmentActiity -> ComponentActivity 组件 ComponentActivty实现了ViewModelStoreOwner和LifecycleOwner接口)

ViewModelStore内部有一个HashMap用来存储ViewModel

所以ViewModelStore里的mViewModelStore即是Activity,ViewModelProvider持有mViewModelStore的引用,ViewModelProvider内部持有mViewModelStore引用

//ViewModelProvider.class
public <T extends ViewModel> T get(@NonNull Class<T> modelClass) {
    ...
    return get(DEFAULT_KEY + ":" + modelClass.getCanonicalName(), modelClass);
}

public <T extends ViewModel> T get(@NonNull String key, @NonNull Class<T> 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;
}

public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
    return modelClass.newInstance();
    ...
}

get方法会创建ViewModel对象,使用Factory工厂来创建 , 工厂通过反射来创建ViewModel。

而由于我们是使用ViewModelProvider(ViewModelStoreOwner))来创建的ViewModelProvider,所以这里使用的ViewModelProvider.Factory其实是ComponentActivity,因为ComponentActivity实现了HasDefaultViewModelProviderFactory接口。

public class ComponentActivity extends androidx.core.app.ComponentActivity implements
...
        HasDefaultViewModelProviderFactory{
    public ViewModelProvider.Factory getDefaultViewModelProviderFactory() {
        ...
        mDefaultFactory = new SavedStateViewModelFactory(
                getApplication(),
                this,
                getIntent() != null ? getIntent().getExtras() : null);
    
        return mDefaultFactory;
    }                                         
}

到此,我们创建的ViewModel实际保存在了mViewModelStore的HashMap里,而mViewModelStore是属于Activity的成员变量。

二、ViewModel的结束

开始:ViewModel的创建上面已经讲的很多了,生命的开始是从我们创建ViewModel开始

结束:而生命的结束就不是由我们自己控制的了,因为我们的ViewModel是存储在Activity的ViewModelStore里,所以生命的结束也是由Activity来处理的。

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) {
                ...
                getViewModelStore().clear();
            }
        }
    });
    ...
}

在创建ComponentActivity的时候,我们注册了Activity的Lifecycle监听,当Activity.onDestory的时候会自动清空所有的ViewModel,并且会给ViewModel一个onCleared回调,告诉它已经没人引用它了。并对

ViewModel里的mBagOfTags挨个调用close方法

//ViewModel.class
protected void onCleared() {
}

@MainThread
final void clear() {
    mCleared = true;
    if (mBagOfTags != null) {
        synchronized (mBagOfTags) {
            for (Object value : mBagOfTags.values()) {
                // see comment for the similar call in setTagIfAbsent
                closeWithRuntimeException(value);
            }
        }
    }
    onCleared();
}

综上所述,ViewModel是有生命周期的,生命的结束由Activity控制

三、总结:

1、ViewModelProvider其实只是一个中介:

ViewModel的创建,工厂ViewModelProvider.Factory实际是由ComponentActivity持有。

ViewModel的存储,ViewModelStore来负责并且由ComponentActivity持有。

ViewModel的销毁,还是由ComponentActivity来负责。

2、ViewModel就是MVVM中的核心VM,由ComponentActivity来负责生命周期和存储。

3、ComponentActivity就是ViewModel的全权大管家。

4、MVVM中的view层是持有viewmodel层引用的。

有关Android MVVM学习之----ViewModel、ViewModelProvider、ComponentActivity的关系的更多相关文章

  1. ruby - Rails 关联 - 同一个类的多个 has_one 关系 - 2

    我的问题的一个例子是体育游戏。一场体育比赛有两支球队,一支主队和一支客队。我的事件记录模型如下:classTeam"Team"has_one:away_team,:class_name=>"Team"end我希望能够通过游戏访问一个团队,例如:Game.find(1).home_team但我收到一个单元化常量错误:Game::team。谁能告诉我我做错了什么?谢谢, 最佳答案 如果Gamehas_one:team那么Rails假设您的teams表有一个game_id列。不过,您想要的是games表有一个team_id列,在这种情况下

  2. [工业相机] 分辨率、精度和公差之间的关系 - 2

    📢博客主页:https://blog.csdn.net/weixin_43197380📢欢迎点赞👍收藏⭐留言📝如有错误敬请指正!📢本文由Loewen丶原创,首发于CSDN,转载注明出处🙉📢现在的付出,都会是一种沉淀,只为让你成为更好的人✨文章预览:一.分辨率(Resolution)1、工业相机的分辨率是如何定义的?2、工业相机的分辨率是如何选择的?二.精度(Accuracy)1、像素精度(PixelAccuracy)2、定位精度和重复定位精度(RepeatPrecision)三.公差(Tolerance)四.课后作业(Post-ClassExercises)视觉行业的初学者,甚至是做了1~2年

  3. ruby - Rails 组合多个 activerecord 关系 - 2

    我想合并多个事件记录关系例如,apple_companies=Company.where("namelike?","%apple%")banana_companies=Company.where("namelike?","%banana%")我想结合这两个关系。不是合并,合并是apple_companies.merge(banana_companies)=>Company.where("namelike?andnamelike?","%apple%","%banana%")我要Company.where("名字像?还是名字像?","%apple%","%banana%")之后,我会写代

  4. ruby-on-rails - Ruby on Rails - has_one 关系,如何检查它是否具有现有关联? - 2

    我有一个简单的问题,与关联有关。我有一个书的模型,它有_onereservation。预订属于_书本。我想在预订Controller的创建方法中确保在预订时没有预订一本书。换句话说,我需要检查该书是否存在任何其他预订。我该怎么做?编辑:Aaa我做到了,感谢大家的提示,学到了一些新东西。当我尝试提供的解决方案时,出现no_method错误或nil_class等。这让我开始思考,我尝试处理的对象根本不存在。Krule给了我使用book.find的想法,所以我尝试使用它。最终我得到了它的工作:book=Book.find_by_id(reservation_params[:book_id])

  5. ruby-on-rails - Ruby/Rails - 检查 HABTM 关系记录中是否存在子 ID - 2

    我有一组名为Tasks和Posts的资源,它们之间存在has_and_belongs_to_many(HABTM)关系。还有一个连接它们的值的连接表。create_table'posts_tasks',:id=>falsedo|t|t.column:post_id,:integert.column:task_id,:integerend所以我的问题是如何检查特定任务的ID是否存在于从@post.tasks创建的数组中?irb(main):011:0>@post=Post.find(1)=>#@post.tasks=>[#,#]所以我的问题是,@post.tasks中是否存在"@task

  6. Ruby On Rails 模型、 View 和 Controller 之间的关系 - 2

    根据我目前的理解,如果我必须描述Rails应用程序的各个组件如何协同工作以响应请求,我会说以下内容:1)路由确定哪些请求URL映射到哪些Controller方法。2)Controller方法从模型中获取信息并将该信息(以全局变量的形式)传递给相应的View模板。3)View模板使用存储在全局变量中的数据来构造最终响应。在上面的解释中,几个组件之间的关系是明确的,不可否认的;即:1)路由和Controller方法2)Controller方法和View模板其实上面的关系是一对一的。但是,模型类与其相邻组件类型(即Controller)的关系并不明确。是的,Controller从模型中检索信

  7. ruby-on-rails - 本地 gem 的“bundle 安装”没有解决依赖关系,而 'gem install' 可以 - 2

    我在目录“/home/enterprise/pkg”中有一个本地gem(enterprise-0.0.1.gem)。它依赖于active_directorygem(v1.5.5),这是在它的enterprise.gemspec文件中指定的,如下所示:-gem.add_dependency("active_directory")在我的应用程序的Gemfile中,我添加了以下行:-gem'enterprise','0.0.1',path=>'/home/enterprise/pkg'当我做的时候bundleinstall在我的应用程序的源目录中,只安装了企业gem。因此,我遇到了引用act

  8. ruby-on-rails - Rails/Mongoid 与 Struct 的关系问题 - 2

    我正在构建这个图书馆应用程序,它有3个类。国家、图书馆和书籍。国家有许多图书馆,图书馆属于一个国家。图书馆有很多书,书是嵌入图书馆的。但是,当我执行此auto_pick_job时,我们到达top_free_book并调用library.state。由于某种原因,library.state为nil。我希望恢复状态但没有骰子。我调用和创建库的方式如下。所以图书馆将永远属于一个现有的国家。state=Stats.find(x)library=state.libaries.new(info)library.save_optimistic!我也很感激使用Struct的关系帮助。classStat

  9. ruby-on-rails - 如何 stub 事件记录关系以使用 rspec 测试 where 子句? - 2

    我有一个看起来像这样的类:classFoo在测试#nasty_bars_present?我想编写一个rspec测试来对bars关联进行stub,但允许where自然执行。像这样的东西:describe"#nasty_bars_present?"docontext"withnastybars"dobefore{foo.stub(:bars).and_return([mock(Bar,bar_type:"Nasty")])}it"shouldreturntrue"doexpect(foo.nasty_bars_present?).tobe_trueendendend上面的测试给出了一个关于

  10. ruby-on-rails - 子文件夹和关系中的 Rails 模型 - 2

    我在自动加载的文件夹中组织了一些Rails模型config.autoload_paths+=Dir[Rails.root.join('app','models','{**}')]我可以直接使用所有模型(例如Image.first.file_name)但是当我尝试通过关系访问它们时,例如@housing.images.eachdo...使用has_many:images我得到以下错误UnabletoautoloadconstantHousing::HousingImage,expected/path/app/models/housing/image.rbtodefineit如何让Rail

随机推荐