Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to instantiate ViewModel In AndroidX?

I want to initialize ViewModel in Activity using androidx library

I have tried what documentation says but it is not working. the ".of" is not resolved.

import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.databinding.DataBindingUtil import androidx.lifecycle.ViewModelProvider import com.example.myapplication.databinding.ActivityMainBinding`  class MainActivity : AppCompatActivity() {      override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         val binding: ActivityMainBinding = DataBindingUtil.setContentView(             this, R.layout.activity_main)         binding.setLifecycleOwner(this)          var model = ViewModelProvider.of(this).get(SheduleViewModel::class.java)      } } 

of is not resolved, maybe there are another way to do it in androidx

like image 271
Nurseyit Tursunkulov Avatar asked Jan 22 '19 17:01

Nurseyit Tursunkulov


People also ask

How does a ViewModel work internally?

ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way.It is the main component in the MVVM architecture. ViewModel can be created with activity context or fragment context. When a ViewModel object is created, it is stored inside Activity OR FragmentManager.

How do I create an instance of a ViewModel in Android?

Create a ViewModel instance in your activity using ViewModelProvider. We need to pass the context and ViewModel class name to ViewModelProvider to get the instance. override fun onCreate (savedInstanceState: Bundle?) { ViewModelProvider is a utility class for ViewModelStore.

Can a ViewModel have a constructor in Android?

If the ViewModel needs the Application context, for example to find a system service, it can extend the AndroidViewModel class and have a constructor that receives the Application in the constructor, since Application class extends Context. ViewModel objects are scoped to the Lifecycle passed to the ViewModelProvider when getting the ViewModel.

How do I get the ViewModel of an activity?

You usually request a ViewModel the first time the system calls an activity object's onCreate () method. The system may call onCreate () several times throughout the life of an activity, such as when a device screen is rotated. The ViewModel exists from when you first request a ViewModel until the activity is finished and destroyed.

How to extend a ViewModel without arguments?

If your view-model is only extending the ViewModel without extra arguments then use the NewInstanceFactory (). // Activity / fragment class private lateinit var viewModel: MyOwnViewModel // onCreate viewModel = ViewModelProvider ( this, ViewModelProvider.NewInstanceFactory () ).get (MyOwnViewModel::class.java)


1 Answers

Updated answer:

Things changed a little bit, as the previously needed dependency - ViewModelProviders - got deprecated (see the old answer for details). You can now use the ViewModelProvider constructor directly.

So, in this case, the answer would be:

private val viewModel = ViewModelProvider(this).get(SheduleViewModel::class.java) 

Note that, however, if you include the androidx.activity:activity-ktx:$Version dependency (a few of the commonly used AndroidX dependencies already include it for you), you can make use of property delegation:

private val viewModel: SheduleViewModel by viewModels() 

Which internally will use ViewModelProvider and scope your ViewModel to your Activity. It's just a more concise way of writing the same thing. You can do the same for a Fragment by including the androidx.fragment:fragment-ktx:$Version dependency instead (again, commonly already included by other AndroidX dependencies).

Both the ViewModelProvider constructor and by viewModels() also accept a factory as a parameter (useful for injecting your ViewModel):

private val viewModel =      ViewModelProvider(this, viewModelFactory).get(SheduleViewModel::class.java) 

and

private val viewModel: SheduleViewModel by viewModels { viewModelFactory } 

Use the one that best suits you.

Old answer:

Add the androidx.lifecycle:lifecycle-extensions:$lifecycleExtensionsVersion dependency in order to import ViewModelProviders.

like image 98
Ricardo Costeira Avatar answered Sep 30 '22 19:09

Ricardo Costeira