Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get other fragment's ViewModel

I have an external event that changes several fragments state, As I am using Android architecture components I've created several ModelViews for every fragment.

What is the right way to send messages between ModelViews

like image 477
Andrew Matiuk Avatar asked Nov 22 '25 16:11

Andrew Matiuk


1 Answers

I am assuming you are using viewModel to preserve state across activity and fragment recreations. You do realize that a viewModel is essentially your model class, right? So why would you want to send messages between viewModels?

If you have an external event that changes a fragment's state, you should propagate it to your activities who will then send those message to your fragments where you can update your view model's state.

like image 175
Susheel Avatar answered Nov 25 '25 07:11

Susheel