本文介绍了在片段之间共享ViewModels或在ViewModels之间共享LiveData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想要一种正确在片段之间共享ViewModel或在ViewModels之间共享LiveData的方法.

Basically I want a way to properly share ViewModels between fragments OR share LiveData between ViewModels.

我的情况:

我有2个片段(FragmentA& FragmentB)-每个片段都有自己的ViewModel:

I have 2 fragments (FragmentA & FragmentB) - each has its own ViewModels:

FragmentA具有ViewModelAFragmentB具有ViewModelB.

ViewModelA具有LiveDataA1ViewModelB具有LiveDataB1LiveDataB2

ViewModelB仅允许具有LiveDataB2,而ViewModelA不能具有.

ViewModelB is only allowed to have LiveDataB2 and ViewModelA cannot have it.

问题是我希望FragmentA观察ViewModelB中的LiveDataB2.

方法1:

除了ViewModelA之外,ViewModelB也用在FragmentA中(因此就像FragmentA中的2个ViewModels).因此FragmentA将观察ViewModelB中的LiveDataB2.

Aside from ViewModelA, ViewModelB is also be used in FragmentA (so it's like 2 ViewModels in FragmentA).So FragmentA will observe LiveDataB2 from ViewModelB.

这是我目前的实现方式.但是我觉得拥有另一个用于其他片段的ViewModel是不合适的.我认为每个Fragment应该只有1个ViewModel.

This is my current implementation now. But I feel like it's not proper to have another ViewModel that is intended for other fragments.I thinking that each Fragment should only have 1 ViewModel.

方法2:

创建一个新的SharedViewModel.因此,我们现在将拥有3个ViewModel:ViewModelA具有LiveDataA1ViewModelB具有LiveDataB1SharedViewModel具有LiveDataB2.(在这里,我将LiveDataB2ViewModelB移到了SharedViewModel)

Create a new SharedViewModel.So we will have 3 ViewModels now:ViewModelA has LiveDataA1, ViewModelB has LiveDataB1, SharedViewModel has LiveDataB2.(Here I move LiveDataB2 from ViewModelB to SharedViewModel)

除了ViewModelA以外,在FragmentA中也使用SharedViewModel.因此FragmentA将观察SharedViewModel中的LiveDataB2.

Aside from ViewModelA, SharedViewModel is also be used in FragmentA.So FragmentA will observe LiveDataB2 from SharedViewModel.

所以我想它和#1一样,但是我想在这里我想SharedViewModel只是一个实用的ViewModel,就像获取所需的共享数据一样.所以在这里,我们就像将所有可以在FragmentA和FragmentB(甚至与其他片段)之间共享/共享的LiveData一样.

So I guess its the same as #1 but I guess but here I'm thinking that SharedViewModel is just a util ViewModel to just like get the shared data needed.So here we are like putting all the LiveDatas that can be common/shared between FragmentA and FragmentB (or even with other fragments)

方法3:

在ViewModel之间共享LiveData.我认为这太疯狂了,我不知道如何实现.但是我认为ViewModelA中将有一个新的LiveDataA2,它与ViewModelB中的LiveDataB2所引用的实例相同.

Share LiveData between ViewModels.I think this is wild and I don't know how to implement this.But I'm thinking that there will a new LiveDataA2 in ViewModelA that refers to the same instance as LiveDataB2 in ViewModelB.

因此,FragmentA仅具有ViewModelA,并且可以观察LiveDataA2.如果ViewModelB中的LiveDataB2中有更改,则FragmentA将具有该更改.

So FragmentA will only have ViewModelA and can observe LiveDataA2.If there is a change in LiveDataB2 in ViewModelB, FragmentA will have it.

在这里几乎需要一些建议,以正确的方式使用!

推荐答案

共享ViewModel 是正确的方法

对于具有多个实时数据对象LiveDataA1LiveDataB1LiveDataB2的两个片段FragmentAFragmentB,您应该使用单个Shared ViewModel,通过这种方法,您的FragmentA可以轻松观察.

You should use a single Shared ViewModel for both fragments FragmentA and FragmentB with multiple live data objects LiveDataA1, LiveDataB1 and LiveDataB2, with this approach your FragmentA can easily observe LiveDataB2.

现在,您的方法存在的问题是:

Now, the problem with your approaches are:

方法#1:

如果FragmentA创建ViewModelB的实例,您将不会与FragmentB关联ViewModelB,因此ViewModelB会丢失其状态.

If FragmentA creates an instance of ViewModelB you will not get ViewModelB associate with FragmentB so ViewModelB lost its state.

方法2:

问题是,当有推荐的方法(例如,根据官方文档Shared ViewModel)时,您正在创建单独的ViewModel

Somehow the problem is you are creating separate ViewModel when there is a recommended way i.e Shared ViewModel as per official docs

方法3:

共享LiveData对象不是正确的方法,因为它与生命周期所有者相关联.

Sharing LiveData objects is not the right approach as it is associated with lifecycle owner.

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

09-09 08:32