-1

In this code I am creating a reference to the GameViewModel class which extends the ViewModel class I know why we are delegating the responsibility to viewModels() but I want to know what actually is this viewModels and how it is different from ViewModel

private val viewModel: GameViewModel by viewModels()
  • I think this might give you some ideas: https://stackoverflow.com/questions/65352324/view-model-initialization-using-by-viewmodels-vs-viewmodelproviderthis-ge – denis_lor Mar 05 '21 at 18:06

1 Answers1

1

You compare kotlin delegate function( by viewModels()) and ViewModel class together, open sources i think that will help you better understand.

Rob Gas
  • 56
  • 4
  • can u pls elaborate a bit – gaurav_rajput Mar 05 '21 at 20:43
  • 1
    Yes of course , i guess you know about MVVM architecture , so ViewModel is VM, before MVVM we used MVC or MVP , ViewModel(VM) has a bit more benefits than Controller (C) or Presenter (P), For example ViewModel can survive configurationChange( Screen Rotation, Screen Size , Theme) changes, https://developer.android.com/topic/libraries/architecture/viewmodel check out this link for more details about ViewModel. Now i wanna talk about viewModels(), viewModels() is a kotlin delegate method which are creating a new instance of ViewModel, so thats all. I hope I've been of help. – Rob Gas Mar 05 '21 at 21:34