In kotlin how to use ViewModel With ViewModelProvider.AndroidViewModelFactory?
Asked Answered
D

6

6

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?

Delitescence answered 2/2, 2019 at 4:17 Comment(2)
Can you share piece of code that you're using to create ViewModel instance?Amick
mViewModel = ViewModelProviders.of(this).get(MainViewModel::class.java)Delitescence
M
9

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)
Metallography answered 4/1, 2020 at 6:57 Comment(0)
T
4

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

}
Torrens answered 2/2, 2019 at 5:50 Comment(3)
yeah i use this code but how to use ViewModelProvider.AndroidViewModelFactory?Delitescence
Updated my answer.Torrens
This class is deprecated, use ViewModelProvider (without the s)Pernod
R
3

you can try this code

    ViewModelProvider.AndroidViewModelFactory.getInstance(application).create(UserViewModel::class.java)
Reincarnation answered 21/8, 2019 at 10:47 Comment(0)
T
2

You can use AndroidViewModelFactory like this:

mViewModel = ViewModelProvider(this,
            ViewModelProvider.AndroidViewModelFactory(application))
            .get(MainViewModel::class.java)
Tupi answered 22/11, 2020 at 16:26 Comment(0)
T
1

So, it is quite simple:

// This is the deprecated way to create a viewModel.
viewModel = ViewModelProviders.of(owner).get(MyViewModel::class.java)

//This is the deprecated way to create a viewModel with a factory.
viewModel = ViewModelProviders.of(owner, mViewModelFactory).get(MyViewModel::class.java)

// This is the new way to create a viewModel.
viewModel = ViewModelProvider(owner).get(MyViewModel::class.java)

//This is the new way to create a viewModel with a factory.
viewModel = ViewModelProvider(owner, mViewModelFactory).get(MyViewModel::class.java)
Taction answered 27/10, 2019 at 8:36 Comment(0)
T
1
  1. Open build.gradle(Module:~.app)

  2. Edit appcompat version to 1.3.0-alpha02

     implementation 'androidx.appcompat:appcompat:1.3.0-alpha02'
    
Tupi answered 1/11, 2020 at 18:8 Comment(1)
My another answer using AndroidViewModelFactory is better than this, I think.Tupi

© 2022 - 2024 — McMap. All rights reserved.