SharedViewModel的实例化

huangapple go评论105阅读模式
英文:

Instantiation of SharedViewModel

问题

I follow the coding in Flow tutorials. In one of them (communication between 2 fragments with a shared ViewModel), there is a deprecated method ViewModelProviders.of(...):

  1. public void onActivityCreated(@Nullable Bundle savedInstanceState) {
  2. super.onActivityCreated(savedInstanceState);
  3. viewModel = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
  4. viewModel.getText().observe(getViewLifecycleOwner(), new Observer<CharSequence>() {
  5. @Override
  6. public void onChanged(@Nullable CharSequence charSequence) {
  7. editText.setText(charSequence);
  8. }
  9. });
  10. }

The author explained that the app knows when to destroy and recreate the ViewModel, so we shouldn't do it. I found a method which replaces the deprecated ViewModelProviders:

  1. // myNewMethod
  2. viewModel = ViewModelProvider.AndroidViewModelFactory.getInstance(getActivity().getApplication()).create(SharedViewModel.class);

The problem is now that this method always creates a new instance of Shared ViewModel. So when I have two fragments, the method is called twice and generates two different shared ViewModels!

How to get rid of this, and where should I instantiate the Shared ViewModel? Main activity or still in the fragment?

英文:

i follow the coding in Flow tutorials. In one of them (communication between 2 fragments with shared ViewModel there is a depracated method ViewModelProviders.of(...:

  1. public void onActivityCreated(@Nullable Bundle savedInstanceState) {
  2. super.onActivityCreated(savedInstanceState);
  3. viewModel = ViewModelProviders.of(getActivity()).get(SharedViewModel.class);
  4. viewModel.getText().observe(getViewLifecycleOwner(), new Observer<CharSequence>() {
  5. @Override
  6. public void onChanged(@Nullable CharSequence charSequence) {
  7. editText.setText(charSequence);
  8. }
  9. });
  10. }

The author explained that app knows when to destroy and recreate ViewModel so we shouldn't do it. I found a method which replaces the depracated ViewModelProviders:

  1. //myNewMethod
  2. viewModel= ViewModelProvider.AndroidViewModelFactory.getInstance(getActivity().getApplication()).create(SharedViewModel.class);

The problem is now, that this method create always a new instance of Shared ViewModel. So when i have two fragments, the method is called twice and generate two different shared ViewModels !

how to get rid of this and where should i instantiate the Shared ViewModel ? Main acitvity or still in fragment?

答案1

得分: 2

问题已解决。我应该使用 ViewModel 而不是 AndroidViewModel,并使用以下方式实例化:

  1. ViewModel = new ViewModelProvider(requireActivity()).get(SharedViewModel.class);
英文:

Problem solved. I should use ViewModel instead of AndroidViewModel And instantiate with :

  1. ViewModel= new ViewModelProvider(requireActivity()).get(SharedViewModel.class);

huangapple
  • 本文由 发表于 2020年8月11日 03:09:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63346531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定