Home > Back-end >  Use viewmodel for a fragment called twice
Use viewmodel for a fragment called twice

Time:01-01

I have a navigation as follows:

FragmentList -> FragmentDetail -> FragmentDetail -> FragmentDetail

I use a viewModel for the detail FragmentDetailViewModel

private val detailViewModel: DetailViewModel by activityViewModels()

But if I go forward, and then back, the above fragmentDetails are changed. How to have a viewmodel assigned to the fragment without changing the others?

Thanks a lot

CodePudding user response:

It sounds like you want to link the viewmodel to the fragment.

ViewModelProvider(this,viewModelFactory).get(MyViewModel.class)

CodePudding user response:

With this way that you initialized view model, view model depends on the parent activity you must use this way

private val detailViewModel: DetailViewModel by viewModels()

to your view model instance depends on your fragment instance.

  • Related