viewmodelproviders

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

帅比萌擦擦* 提交于 2020-05-27 09:27:12
问题 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 depreceated. So according to this question on stackoverflow: ViewModelProviders is deprecated in 1.1.0 main activity in onCreate, I do the following (and I could swear I already had it running): mainActivityViewModel = new ViewModelProvider(this).get

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

徘徊边缘 提交于 2020-05-27 09:25:26
问题 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 depreceated. So according to this question on stackoverflow: ViewModelProviders is deprecated in 1.1.0 main activity in onCreate, I do the following (and I could swear I already had it running): mainActivityViewModel = new ViewModelProvider(this).get

Cannot create instance of class ViewModel while using MVVM

流过昼夜 提交于 2020-04-18 06:12:09
问题 I have created ViewModel class, and trying to create an instance of my ViewModel class using ViewModelProvider. At runtime, I'm getting this error "Cannot create an instance of class className ViewModel. Here are the required codes: MainActivity.java public class MainActivity extends AppCompatActivity { MainActivityViewModel mainActivityViewModel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

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

て烟熏妆下的殇ゞ 提交于 2019-12-21 17:09:55
问题 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 ?: this).get(MainViewModel::class.java) } None of the following functions can be called with the arguments supplied. of(Fragment) defined in androidx.lifecycle.ViewModelProviders of(FragmentActivity) defined in androidx.lifecycle.ViewModelPro It seems the language does