问题描述
基本上,我想要一种正确在片段之间共享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
具有ViewModelA
,FragmentB
具有ViewModelB
.
ViewModelA
具有LiveDataA1
,ViewModelB
具有LiveDataB1
和LiveDataB2
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
具有LiveDataA1
,ViewModelB
具有LiveDataB1
,SharedViewModel
具有LiveDataB2
.(在这里,我将LiveDataB2
从ViewModelB
移到了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 是正确的方法
对于具有多个实时数据对象LiveDataA1
,LiveDataB1
和LiveDataB2
的两个片段FragmentA
和FragmentB
,您应该使用单个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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!