-
Notifications
You must be signed in to change notification settings - Fork 0
ViewModelUtils
-
ParametrizedFactory(private val T: Any) : ViewModelProvider.NewInstanceFactory()
Returns a new object ofViewModel
class with single constructor parameter.
Usage
val viewModel = ViewModelProviders.of(this,ParametrizedFactory(<objectToPassToViewModel>)).get(<YourViewModel>::class.java)
-
MultipleParametrizedFactory(private var constructorParams: Array<out Any>) : ViewModelProvider.NewInstanceFactory()
Returns a new object ofViewModel
class with multiple constructor parameter.
Usage
val viewModel = ViewModelProviders.of(this,MultipleParametrizedFactory(<array of objects>)).get(<YourViewModel>::class.java)
Or you can call below method for direct initialization. But it requires AppCompactActivity, FragmentActivity, DialogFragment and Fragment Scope for calling i.e., it can be called from this classes only
-
initViewModel(constructorObject: Any, viewModelClass: Class<V>):V
Return your provided viewModel with single parameter. Here V is your ViewModel class. Pass any constructor inconstructorObject
-
initViewModel(constructorObject: Array<out Any>, viewModelClass: Class<V>):V
Return your provided viewModel with multiple parameter. Here V is your ViewModel class. Pass array of constructors inconstructorObject
Usage
val viewModel = initViewModel(customObject,MyViewModel::class.java)
OR
val viewModel = initViewModel(YourViewModel(YourViewModelConstructor), YourViewModel::class.java)