简体   繁体   English

直接实例化ViewModels,而不使用ViewModelProviders.of方法

[英]Instantiate ViewModels directly, without making use of ViewModelProviders.of method

I have a ViewModel called RecipesViewModel . 我有一个名为RecipesViewModel的ViewModel。 Usually, I instantiated it this way: 通常,我这样实例化:

RecipesViewModel viewModel = ViewModelProviders.of(this, new ViewModelProvider.Factory() {
            @Override
            public <T extends ViewModel> T create(Class<T> modelClass) {
                return (T) new RecipesViewModel(recipesRepository);
            }
        }).get(RecipesViewModel.class);

But now I'm using dagger2 and so I put a @Inject annotation on the constructor of this ViewModel, so I'm able to inject it directly in my fragment, using field injector. 但是现在我正在使用dagger2,所以我在这个ViewModel的构造函数上放了一个@Inject注释,所以我可以使用字段注入器将它直接注入到我的片段中。

My question is: do I lose something starting the viewmodel this way instead of ViewModelProviders.of way? 我的问题是:我是否会以这种方式而不是ViewModelProviders.of方式丢失一些启动viewmodel的东西? My ViewModel is already Scoped, so only one instance is create in context. 我的ViewModel已经被Scoped,因此在上下文中只创建一个实例。

Other option is to move only the factory instantiation to a dagger2 module, but if there is no problem I prefer the first aproach. 其他选项是仅将工厂实例化移动到dagger2模块,但如果没有问题,我更喜欢第一个方法。

-- EDIT -- - 编辑 -

Reading the documentation android.arch.lifecycle.ViewModel , I'm a little more afraid. 阅读文档android.arch.lifecycle.ViewModel ,我有点害怕了。 Whe use ViewModelProviders.of to provide a Scope (fragment or activity). 使用ViewModelProviders.of提供Scope(片段或活动)。 If I instantiate it directly what will be the Scope? 如果我直接实例化它将是什么范围?

ViewModel is a class that is responsible for preparing and managing the data for an Activity or a Fragment. ViewModel是一个负责准备和管理Activity或Fragment数据的类。 It also handles the communication of the Activity / Fragment with the rest of the application (eg calling the business logic classes). 它还处理Activity / Fragment与应用程序其余部分的通信(例如,调用业务逻辑类)。

A ViewModel is always created in association with a scope (an fragment or an activity) and will be retained as long as the scope is alive. 始终与范围(片段或活动)关联创建ViewModel,并且只要范围处于活动状态,就会保留ViewModel。 Eg if it is an Activity, until it is finished. 例如,如果它是一个活动,直到它完成。

In other words, this means that a ViewModel will not be destroyed if its owner is destroyed for a configuration change (eg rotation). 换句话说,这意味着如果ViewModel的所有者因配置更改(例如旋转)而被销毁,则不会销毁它。 The new instance of the owner will just re-connected to the existing ViewModel. 所有者的新实例将重新连接到现有的ViewModel。

-- /EDIT -- - /编辑 -

The RecipesViewModel code is showing below: RecipesViewModel代码如下所示:

@PerActivity
public class RecipesViewModel extends ViewModel {
    private static final String TAG = "RecipesViewModel";
    private final RecipesRepository recipesRepository;

    private LiveData<List<Recipe>> recipes = null;

    @Inject
    public RecipesViewModel(RecipesRepository recipesRepository) {
        this.recipesRepository = recipesRepository;
    }

    public final void loadAll() {
        recipes = recipesRepository.getRecipes();
    }

    public LiveData<List<Recipe>> getRecipes() {
        return recipes;
    }
}

For me right now (and I need to research this), but injecting a view model instead of using the ViewModelProviders functionality means you lose some easy activity-fragment communication. 对我来说(我需要研究这个),但是注入视图模型而不是使用ViewModelProviders功能意味着你会丢失一些简单的活动片段通信。

For example from the docs they provide an example of an activity hosting 2 fragments. 例如, 从文档中,他们提供了托管2个片段的活动的示例。 If one fragment needs to talk to another, the previous method was to maintain an interface via the activity who also had to take care of the lifecycle of that interface. 如果一个片段需要与另一个片段交谈,那么之前的方法是通过活动维护一个接口,该活动也必须处理该接口的生命周期。 Instead now you can just fetch it from the the ViewModelProviders 'repo' whenever you need. 相反,现在您可以随时从ViewModelProviders'repo'中获取它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:找不到方法ViewModelProviders.of(Fragment,Factory)的合适方法 - error: no suitable method found for method ViewModelProviders.of(Fragment,Factory) ViewModelProviders.of(getActivity()) “无法解析(android.app.Activity)的方法” - ViewModelProviders.of(getActivity()) “Cannot resolve method of(android.app.Activity)” Android Studio,AndroidX,ViewModelProviders.of(this)上的“无法解析方法。” - Android Studio, AndroidX, “Cannot resolve method..” on ViewModelProviders.of(this) 为什么我应该为视图模型使用 viewmodelproviders? - Why should I use viewmodelproviders for viewmodels? ViewModelProviders.of(activity) 只接受片段或片段活动 - ViewModelProviders.of(activity) only accept fragment or fragmentactivity Kotlin 不理解 ViewModelProviders.of(activity ?: fragment) - Kotlin does not understand ViewModelProviders.of(activity ?: fragment) 当我尝试实例化ViewModelProviders时,无法解析of()方法 - cannot resolve method of() , when i try to instantiate ViewModelProviders 我无法在 ViewModelProviders.of() function 中将片段作为参数 - I can't give fragment as argument inside ViewModelProviders.of() function 由于不推荐使用 ViewModelProviders.of(),我应该如何创建 ViewModel 的 object? - As ViewModelProviders.of() is deprecated, how should I create object of ViewModel? 如何在Non-FragmentActivity上使用ViewModelProviders.of()获取ViewModel? - How to fetch the ViewModel using ViewModelProviders.of() on non-FragmentActivity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM