Questions tagged [viewmodelproviders]

31 questions
47
votes
10 answers

How to get an Instance of ViewModel in activity in 2020/21?

I am new to the mvvm pattern. I created a ViewModel for the main activity. Now I want to get an instance of the ViewModel in the main activity. Most Tutorials and answers here on Stackoverflow suggest using ViewModelProviders.of(..., but this is…
Gerke
  • 926
  • 1
  • 10
  • 20
7
votes
3 answers

ViewModelProviders is deprecated in 2.2.0

Currently I update androidx.lifecycle:lifecycle-extensions version from 2.2.0-alpha01 to 2.2.0and it shows that ViewModelProviders is depricated. So what is the alternative way to use ViewModelProviders in kotlin?
6
votes
3 answers

Kotlin does not understand ViewModelProviders.of(activity ?: fragment)

Inside my Fragment I initialize a ViewModel using ViewModelProviders. I want it to take its Activity if not null, otherwise itself (Fragment). private val viewModel: MainViewModel by lazy { ViewModelProviders.of(activity ?:…
shkschneider
  • 17,833
  • 13
  • 59
  • 112
3
votes
2 answers

Dynamic parameters in viewmodel Android

How can I reuse the parameter of a ViewModel in Android? I have a recycle viewer filled with data from a database based on a parameter: selectedDate. How can I reuse it in a fragment, in this way to refresh the recycle viewer? So far I'm using a…
Erjon
  • 923
  • 2
  • 7
  • 32
3
votes
2 answers

NoClassDefFoundError at AppModule.provideViewModelFactories(AppModule.kt)

I am facing this crash only on Kitkat device. My logcat is below. Crashed at getViewModel Fatal Exception: java.lang.NoClassDefFoundError: kotlin.jvm.internal.Intrinsics at…
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
3
votes
1 answer

java.lang.RuntimeException: Cannot create an instance of class ViewModel, when android recreates Activity and Fragment

Case is of Activity-Fragment Recreate : I am creating ViewModel from activity context at activity level and trying to access the initialised ViewModel at Fragment level, using ViewModelProvider.of(). Below code works in happy case; but when activity…
2
votes
1 answer

As ViewModelProviders.of() is deprecated, how should I create object of ViewModel in the Activity?

error: cannot find symbol MainActivityViewModel model = ViewModelProvider(this).get(MainActivityViewModel.class); I tried different solutions available but no luck for me.
2
votes
3 answers

'ViewModelProviders' is deprecated. Java solution?

'ViewModelProviders' is deprecated from androidx.lifecycle:lifecycle-*:2.2.0-alpha03 https://developer.android.com/jetpack/androidx/releases/lifecycle#2.2.0-alpha03 Here is solution for Kotlin 'ViewModelProviders' is deprecated. After upgrading…
A.M.
  • 215
  • 1
  • 10
1
vote
0 answers

Flutter on page display, need to call a API method and show progress

I want to call a API method when the page initialized. Need to display progress when the future function is waiting, i tried with Future builder but problem is everytime when there is change the future builder function called again. class…
abhi
  • 54
  • 8
1
vote
1 answer

I can't give fragment as argument inside ViewModelProviders.of() function

I want to define ViewModel in fragment but the function is not working properly. I created a ViewModel class as FeedViewModel . And now I want to define it in fragment but the function is not working properly.I checked out the gradle file but…
1
vote
0 answers

I am getting this error - Inheritance from an interface with '@JvmDefault' members is only allowed with -Xjvm-default option

I am getting the above mentioned error. can someone help in resolving this error?
hds
  • 11
  • 1
1
vote
1 answer

Dagger 2: Field Injection without any provider

While working on a project that uses Dagger 2 for DI, I tried to create a ViewModel and field inject it even without creating any provider from any module. Biggest confusing moment for me since as far as I know Dagger needs to know how to construct…
Bitwise DEVS
  • 2,858
  • 4
  • 24
  • 67
1
vote
2 answers

What is the difference between passing"this" and "activity!!" as a ViewModelStoreOwner while creating ViewModelProvider instance

Hi I am a Kotlin learner, wanted to understand the difference between passing"this" and "activity!!" as a ViewModelStoreOwner while creating ViewModelProvider instance in a fragment for ex viewModel = ViewModelProvider( this, …
1
vote
2 answers

ViewModelProviders.of(FragmentActivity).get(ViewModel::class.java) asking for activity instead of viewmodel in get() call

ViewModelProviders.of(this).get() is failing for unforeseen reasons This is being called in my main activity which inherits from FragmentActivity. I'm actually getting an error on the .get() call. The error says it's expecting an activity instead of…
Anthony Porter
  • 152
  • 1
  • 9
0
votes
1 answer

viewModel with repository even following docs - error java.lang.RuntimeException: Cannot create an instance of class

Complete project: https://github.com/giseletoledo/MonitorSaude/tree/master/app/src/main/java/com/oceantech/monitorsaude Hi, I tried many solutions, the first was the official documentation developer android, but didn't work, I don't know if the…
gise
  • 13
  • 2
  • 10
1
2 3