ViewModel for Fragment instead accessing Activity ViewModel?

后端 未结 4 1981
醉话见心
醉话见心 2021-01-30 05:35

The problem is quite straightforward. The question is in context of using ViewModels, LiveData and other related Lifecycle aware arch approaches.
I have an Activity with Na

4条回答
  •  误落风尘
    2021-01-30 06:21

    First there is no harm in having multiple ViewModel's for a single View.

    I would think about my ViewModel's like what kind of data is getting and manipulating, and group them in a way, that seems natural.

    For your case, if the fragments and the activity's logic is very similar, I think you can go with a single ViewModel, but I would avoid that.

    What I would do is break the activity's ViewModel into smaller parts and reuse the proper ViewModel's in my Fragments, so that I wouldn't have a God ViewModel, nor roughly the same code in different ViewModel's.

提交回复
热议问题