Android LiveData - 如何在不同的活动上重用相同的 ViewModel? [英] Android LiveData - how to reuse the same ViewModel on different activities?

查看:29
本文介绍了Android LiveData - 如何在不同的活动上重用相同的 ViewModel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例视图模型:

public class NameViewModel extends ViewModel {
    // Create a LiveData with a String
    private MutableLiveData<String> mCurrentName;

    public MutableLiveData<String> getCurrentName() {
        if (mCurrentName == null) {
            mCurrentName = new MutableLiveData<>();
        }
        return mCurrentName;
    }

}

主要活动:

mModel = ViewModelProviders.of(this).get(NameViewModel.class);

// Create the observer which updates the UI.
final Observer<String> nameObserver = textView::setText;

// Observe the LiveData, passing in this activity as the LifecycleOwner and the observer.
mModel.getCurrentName().observe(this, nameObserver);

我想在第二个活动中调用 mModel.getCurrentName().setValue(anotherName); 并使 MainActivity 接收更改.这可能吗?

I want to call mModel.getCurrentName().setValue(anotherName); in second activity and make MainActivity receive changes. Is that possible?

推荐答案

当你调用 ViewModelProviders.of(this) 时,你实际上创建/保留了一个 ViewModelStore绑定到 this,所以不同的 Activity 有不同的 ViewModelStore 并且每个 ViewModelStore 使用一个 ViewModel 创建一个不同的实例给定工厂,因此您不能在不同的 ViewModelStore 中拥有相同的 ViewModel 实例.

When you call ViewModelProviders.of(this), you actually create/retain a ViewModelStore which is bound to this, so different Activities have different ViewModelStore and each ViewModelStore creates a different instance of a ViewModel using a given factory, so you can not have the same instance of a ViewModel in different ViewModelStores.

但是您可以通过传递一个作为单例工厂的自定义 ViewModel 工厂的单个实例来实现这一点,因此它始终会在不同的 Activity 之间传递您的 ViewModel 的相同实例.

But you can achieve this by passing a single instance of a custom ViewModel factory which acts as a singleton factory, so it will always pass the same instance of your ViewModel among different activities.

例如:

public class SingletonNameViewModelFactory extends ViewModelProvider.NewInstanceFactory {


    NameViewModel t;

    public SingletonNameViewModelFactory() {
      //  t = provideNameViewModelSomeHowUsingDependencyInjection
    }

    @Override
    public NameViewModel create(Class<NameViewModel> modelClass) {
        return t;
    }
}

所以你需要的是使 SingletonNameViewModelFactory 成为单例(例如使用 Dagger)并像这样使用它:

So what you need is to make SingletonNameViewModelFactory singleton (e.g. using Dagger) and use it like this:

mModel = ViewModelProviders.of(this,myFactory).get(NameViewModel.class);

注意:

在不同范围内保留 ViewModel 是一种反模式.强烈建议保留您的数据层对象(例如,使您的 DataSource 或 Repository 成为单例)并在不同范围(活动)之间保留您的数据.

Preserving ViewModels among different scopes is an anti-pattern. It's highly recommended to preserve your data-layer objects (e.g. make your DataSource or Repository singleton) and retain your data between different scopes (Activities).

阅读这篇文章了解详情.

这篇关于Android LiveData - 如何在不同的活动上重用相同的 ViewModel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆