I just learned about liveata viewmodel. I have an object, the main_activity consists of 2 fragments, in which when I change the value of ControlFragment, the value on DetailFragment will change. But It not work , while I change the value in fragment control, the value in fragment 2 does not change.
Please, help me fix issue
ViewModelMedal
public class ViewModelMedal extends ViewModel {
public MutableLiveData<Integer> numberGold = new MutableLiveData<>();
public MutableLiveData<Integer> numberSilver = new MutableLiveData<>();
public MutableLiveData<Integer> numberBronze = new MutableLiveData<>();
public ViewModelMedal() {
numberGold.setValue(0);
numberSilver.setValue(0);
numberBronze.setValue(0);
}
}
ControlFragment.java
public class ControlFragmentJava extends Fragment implements View.OnClickListener {
private Button btnGold, btnGoldPlus, btnSilver, btnSilverPlus, btnCu, btnCuPlus;
private TextView tvGold, tvSilver, tvBronze;
private ViewModelMedal mViewModelMedal;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_control, container, false);
mViewModelMedal = ViewModelProviders.of(this).get(ViewModelMedal.class);
.
.
.
return view;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnBronzePlus:
mViewModelMedal.numberCBronze.setValue(mViewModelMedal.numberCu.getValue()+1);
initSetData();
break;
case R.id.btnBronzeMinus:
mViewModelMedal.numberBronze.setValue(mViewModelMedal.numberCu.getValue()-1);
initSetData();
break;
case R.id.btnGoldMinus:
mViewModelMedal.numberGold.setValue(mViewModelMedal.numberGold.getValue()-1);
initSetData();
break;
case R.id.btnGoldPlus:
mViewModelMedal.numberGold.setValue(mViewModelMedal.numberGold.getValue()+1);
initSetData();
break;
case R.id.btnSilverMinus:
mViewModelMedal.numberSilver.setValue(mViewModelMedal.numberSilver.getValue()-1);
initSetData();
break;
case R.id.btnSilverPlus:
mViewModelMedal.numberSilver.setValue(mViewModelMedal.numberSilver.getValue()+1);
initSetData();
break;
}
}
}
DetailFragment
public class DetailFragment extends Fragment {
public ViewModelMedal mViewModelMedal;
private TextView tvGold, tvSilver, tvBronze;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_detail,container,false);
tvBronze = view.findViewById(R.id.tvDetailBronzeNumber);
tvGold = view.findViewById(R.id.tvDetailGoldNumber);
tvSilver = view.findViewById(R.id.tvDetailSilverNumber);
mViewModelMedal = ViewModelProviders.of(this).get(ViewModelMedal.class);
mViewModelMedal.numberBronze.observe(this, new Observer<Integer>() {
@Override
public void onChanged(Integer integer) {
tvCu.setText(String.valueOf(mViewModelMedal.numberCu.getValue()));
}
});
mViewModelMedal.numberGold.observe(this, new Observer<Integer>() {
@Override
public void onChanged(Integer integer) {
tvGold.setText(String.valueOf(mViewModelMedal.numberGold.getValue()));
}
});
mViewModelMedal.numberSilver.observe(this, new Observer<Integer>() {
@Override
public void onChanged(Integer integer) {
tvSilver.setText(String.valueOf(mViewModelMedal.numberSilver.getValue()));
}
});
return view;
}
The issue is pretty simple here: you have two instances of the ViewModelMedal
class as you use the two fragments as the ViewModelStoreOwner
in the ViewModelProviders.of(this)
calls. What might help you is using the parent Activity
to manage the view model by calling ViewModelProviders.of(requireActivity())
.
Note that the ViewModelProviders
class is deprecated as stated by the message on its page:
This class is deprecated.
Use the constructors for
ViewModelProvider
directly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.