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

查看:224
本文介绍了在不同 Activity 中的片段之间共享 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,比如说你想让它成为你的应用程序实例.您需要您的应用程序来实现 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)

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

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.

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

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