3

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

1
  • I've been playing a while with this? Any updates? Commented Sep 17, 2017 at 19:38

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

ok, for example in onActivityResult I need to update whole activity and set the text on one of fragments. so I have to do good old findFragment in onActivityResult instead of Model to Model communication?
0

You should use one ViewModel in your activity for your event. You can then observe this view model from your activity and other fragments.

For example, below ViewModel class can wrap your event

public class SharedViewModel extends ViewModel { private final MutableLiveData<Item> selected = new MutableLiveData<Item>(); public void select(Item item) { selected.setValue(item); } public LiveData<Item> getSelected() { return selected; } } 

Then you can subscribe to changes to your event in different fragments like below:

public class DetailFragment extends LifecycleFragment { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SharedViewModel model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class); model.getSelected().observe(this, { item -> // update UI }); } } 

Once subscribed, you can now make changes to your event data which will notify the observing fragments or activity:

public class MasterFragment extends Fragment { private SharedViewModel model; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class); itemSelector.setOnClickListener(item -> { model.select(item); }); } } 

Take a look at sharing data between fragments. Google documentation on android architecture components is limited but good.

1 Comment

actually it's pretty bad solution, as far as SharedViewModel became a God's objects that stores everything and totally unsupportable. Next developers would hate you for this

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.