Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – Is passing MutableLiveData to other ViewModels valid practice?

Posted on November 14, 2022

Solution 1 :

I strongly suggest not to share MutableLiveData between two fragments, if you get a bug it will get super hard to debug because you won’t know who is changing that data and where to cause this weird state.
What I like to do is keep all my MutableLiveData inside the ViewModel and expose only LiveData to the Fragments/View to observe. Every change that needs to happen to the MutableLiveData I do it through a function inside the ViewModel.

private val _sharedData = MutableLiveData<List<Any>>()
val sharedData: LiveData<List<Any>>
    get() = _sharedData

I would put fun changeSharedData inside AB_Flow_ViewModel and place all the logic in one ViewModel instead of two. If the logic needs to be different for FragA and FragB I would use two different methods that will change the same LiveDdata

Problem :

I have a scenario, where two fragments (A and B) need to share a list of objects, which can be mutated from both screens. A and B fragments have a corresponding ViewModel each (A_ViewModel and B_ViewModel). They contain some logic on how the shared data is mutated.

To keep the shared data in tact, I have created AB_Flow_ViewModel, which is activity scoped and contains that shared data wrapped in a MutableLiveData.

I have decided to pass that MutableLiveData object to A and B child fragments as a constructor parameter.

I have never seen such approach anywhere, but it seems to work perfectly for me.

Are there any drawbacks of doing this?

This is the pseudo code for my solution:

// Navigation: (A ->  B)
class A() : Fragment() {

  private val viewModel: A_ViewModel ...

  override fun onViewCreated(
      view: View,
      savedInstanceState: Bundle?
  ) {
    super.onViewCreated(view, savedInstanceState)
    viewModel.sharedData.observe {
      ...
    }
  }

}


class A_ViewModel @Inject  constructor(
    val sharedData: MutableLiveData<List<Any>>
) : ViewModel() {

  fun changeSharedData(newSharedData: List<Any>) {
    sharedData.value = newSharedData
  }
}

@Module
class A_Module {

  @Provides
  @FragmentScoped
  fun provideSharedData(fragment: A) =
      fragment.activityViewModels<AB_Flow_ViewModel>().value.sharedData
}

class B() : Fragment() {

  private val viewModel: A_ViewModel ...

  override fun onViewCreated(
      view: View,
      savedInstanceState: Bundle?
  ) {
    super.onViewCreated(view, savedInstanceState)
    viewModel.sharedData.observe {
      ...
    }
  }
}

@Module
class B_Module {

  @Provides
  @FragmentScoped
  fun provideSharedData(fragment: B) =
      fragment.activityViewModels<AB_Flow_ViewModel>().value.sharedData
}


class B_ViewModel @Inject constructor(
    val sharedData: MutableLiveData<List<Any>>
) : ViewModel() {
  fun changeSharedData(newSharedData: List<Any>) {
    sharedData.value = newSharedData
  }
}

class AB_Flow_ViewModel() : ViewModel() {
  val sharedData = MutableLiveData<List<Any>>()
}

Comments

Comment posted by Jokubas Trinkunas

Thank you for your answer! I am aware, that this is Google proposed solution, but if I do it in this way, the trade off is that my parent ViewModel will become huge and this can be avoided if I do it in a way as described. I agree, with your point, that it might be harder to debug. Also, MutableLiveData is only exposed to A and B ViewModels, but not to the views.

READ  [ANSWERED] android - NavController.OnDestinationChangedListener does not work
Powered by Inline Related Posts

Comment posted by Viktor Petrovski

You can organize your ViewModels per feature they don’t necessarily need to hold all the logic from FragA & FragB. Please mark this answer as accepted if it’s helpful so it can help others too. If you have some more questions please reach out to me I would love to help

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress