Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In kotlin how to use ViewModel With ViewModelProvider.AndroidViewModelFactory?

In my current project I use the next line:

mViewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)

For instance a ViewModel but in https://developer.android.com/reference/android/arch/lifecycle/ViewModelProviders.html#ViewModelProviders() recommend use ViewModelProvider.AndroidViewModelFactory because ViewModelProviders() was deprecated in API level 1.1.0.

any idea for this purpose?

like image 513
RΛUL QUISPE Avatar asked Feb 02 '19 04:02

RΛUL QUISPE


People also ask

How do you use ViewModelProvider in Kotlin?

Use the 'by viewModels()' Kotlin property delegate or ViewModelProvider , passing in the fragment. This function is deprecated. Use the 'by viewModels()' Kotlin property delegate or ViewModelProvider , passing in the activity. of (fragment: Fragment, factory: ViewModelProvider.

What is the use of ViewModelProvider?

Creates ViewModelProvider , which will create ViewModels via the given Factory and retain them in the given store . ViewModelStore : ViewModelStore where ViewModels will be stored.

What is the ViewModel factory and constructor of ViewModel?

What is ViewModel Factory? The factory is responsible to create your instance of ViewModel. If your ViewModel has dependencies and you want to test your ViewModel then you should create your own ViewModelProvider. Factory and passed dependency through ViewModel constructor and give value to the ViewModelProvider.


2 Answers

If you had a simple ViewModel extending AndroidViewModel without any additional constructor parameters, its as follows

  1. Extend AndroidViewModel without any additional constructor parameters
class FooViewModel(application: Application) : AndroidViewModel(application) {}
  1. Create View Model in Activity
val viewModel = ViewModelProvider(this).get(FooViewModel::class.java)

But if you had a ViewModel extending AndroidViewModel with any additional constructor parameters, its as follows

  1. Extend AndroidViewModel with any additional constructor parameters
class FooViewModel(application: Application, foo: Foo) : AndroidViewModel(application) {}
  1. Create a new view model factory extending ViewModelProvider.AndroidViewModelFactory
class FooViewModelFactory(val application: Application, val foo: Foo): ViewModelProvider.AndroidViewModelFactory(application) {

            override fun <T : ViewModel?> create(modelClass: Class<T>): T {
                return FooViewModel(
                    application, foo
                ) as T
            }

     }
  1. Create View Model in Activity
val viewModel = ViewModelProvider(this, FooViewModelFactory(application, foo)).get(FooViewModel::class.java)
like image 104
sm abbas Avatar answered Sep 22 '22 02:09

sm abbas


EDIT: The original question is now irrelevant, as you should no longer use the ViewModelProviders utility class. Instead, you should create a ViewModelProvider instance like so:

val viewModel = ViewModelProvider(thisFragment).get(MyViewModel::class.java)

Original answer below.


ViewModelProviders is just a utility class with static methods, there's no need to instantiate it (there are no instance methods in it anyway), so the constructor being deprecated shouldn't be a concern.

The way you use it is by calling its appropriate of method for your use case, passing in a Fragment or Activity, and then calling get on the ViewModelProvider it returns:

val viewModel = ViewModelProviders.of(thisFragment).get(MyViewModel::class.java)

If you don't provide your own factory in the second parameter of the of method, AndroidViewModelFactory will be used by default. This implementation can either create ViewModel subclasses that have no constructor parameters, or ones that extend AndroidViewModel, like such:

class MyViewModel(application: Application) : AndroidViewModel(application) {

    // use application

}
like image 34
zsmb13 Avatar answered Sep 19 '22 02:09

zsmb13