You can achieve this using a sharedViewModel
across all the fragments.
public class SharedtViewModel extends ViewModel {
private MutableLiveData<String> amount;
public void init()
{
amount = new MutableLiveData<>();
}
public void setAmount(String amount)
{
amount.setValue(amount);
}
public LiveData<String> getAmount() {
return amount;
}
}
Set the amount value from your Activity.
SharedViewModel viewModel = ViewModelProviders.of(this).get(SharedViewModel.class);
viewModel.setAmount(amount);
At the end, in every Fragment, you can observe it as follows:
public class FragmentA extends Fragment {
public void onActivityCreated() {
SharedViewModel model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
model.getAmount().observe(this, { amount ->
// Anything here
});
}
}
CLICK HERE to find out more related problems solutions.