在不同活动中的片段之间共享ViewModel [英] Share ViewModel between fragments that are in different Activity

查看:654
本文介绍了在不同活动中的片段之间共享ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为SharedViewModel的ViewModel:

I have a ViewModel named SharedViewModel:

public class SharedViewModel<T> extends ViewModel {

    private final MutableLiveData<T> selected = new MutableLiveData<>();


    public void select(T item) {
        selected.setValue(item);
    }

    public LiveData<T> getSelected() {
        return selected;
    }
}

我已经基于Google的Arch ViewModel参考页上的SharedViewModel示例实现了它:

I've implemented it based on SharedViewModel example on the Google's Arch ViewModel reference page:

https://developer.android.com/topic/libraries/architecture/viewmodel.html#sharing_data_between_fragments

一个活动中的两个或更多片段需要相互通信是很常见的.这绝不是一件容易的事,因为两者 片段需要定义一些接口描述和所有者 活动必须将两者绑定在一起.而且,两个片段都必须 处理尚未创建另一个片段的情况 可见.

It is very common that two or more fragments in an activity need to communicate with each other. This is never trivial as both fragments need to define some interface description and the owner activity must bind the two together. Moreover, both fragments must handle the case where the other fragment is not yet created or not visible.

我有两个片段,分别称为ListFragmentDetailFragment.

I have two fragments, called ListFragment and DetailFragment.

直到现在,我在名为MasterActivity的活动中使用了这两个片段,并且一切正常.

Until now I used these two fragments inside an activity called MasterActivity, and everything worked well.

我在ListFragment中找到了ViewModel,选择了要在DetailFragment上使用的值.

I got the ViewModel in ListFragment, selected the value to use it on DetailFragment.

mStepSelectorViewModel = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);

但是,现在,在某些情况下,我需要将ListFragment(不同设备配置的布局)添加到称为DetailActivity的其他活动中.有没有一种方法可以类似于上述示例?

However, now, in certain cases, I need that ListFragment (a layout to a different device configuration) will be added to a different activity, called DetailActivity. Is there a way to do that similarly to the above example?

推荐答案

有点晚了,但是您可以使用共享的ViewModelStore完成此操作.片段和活动实现ViewModelStoreOwner接口.在这种情况下,每个实例的片段都有一个存储,活动将其保存在一个静态成员中(我想这样它就可以在配置更改后幸存下来).

A little late but you can accomplish this using a shared ViewModelStore. Fragments and activities implement the ViewModelStoreOwner interface. In those cases fragments have a store per instance and activities save it in a static member (I guess so it can survive configuration changes).

回到共享的ViewModelStore,例如,假设您希望它成为您的Application实例.您需要您的应用程序来实现ViewModelStoreOwner.

Getting back to the shared ViewModelStore, let say for example that you want it to be your Application instance. You need your application to implement ViewModelStoreOwner.

class MyApp: Application(), ViewModelStoreOwner {
    private val appViewModelStore: ViewModelStore by lazy {
        ViewModelStore()
    }

    override fun getViewModelStore(): ViewModelStore {
        return appViewModelStore
    }
}

然后,在您知道需要在活动边界之间共享ViewModel的情况下,您将执行以下操作.

Then in the cases when you know that you need to share ViewModels between activity boundaries you do something like this.

val viewModel = ViewModelProvider(myApp, viewModelFactory).get(CustomViewModel::class.java)

因此,现在它将使用您的应用程序中定义的商店.这样,您就可以共享ViewModels.

So now it will use the Store defined in your app. That way you can share ViewModels.

非常重要.因为在此示例中,ViewModels驻留在您的应用程序实例中,当使用它们的片段/活动被销毁时,它们不会被销毁.因此,您必须将它们链接到将使用它们的最后一个片段/活动的生命周期,或手动销毁它们.

Very important. Because in this example the ViewModels live in your application instance they won't be destroyed when the fragment/activity that uses them gets destroyed. So you will have to link them to the lifecycle of the last fragment/activity that will use them, or manually destroy them.

这篇关于在不同活动中的片段之间共享ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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