‫Welcome back.
‫Instead of sending the name and the email to the main activity and the main activity will create a contact
‫and send it to the database we're going to use the ViewModel instance passed from the Add new contact
‫activity to the Add new contact Click handler and use it to insert it into our database.
‫So there is no need to put extra because this method using the intents and put extra will cause some
‫errors when calling the main activity and sending it and the main activity will insert the database.
‫I'll commend this and I'll show you a better approach.
‫I'll create a new ViewModel instance in the add new contact activity before the handler.
‫My ViewModel, my ViewModel equals new ViewModel provider and I'll pass this as the owner dot get and
‫here I need to get my ViewModel class here.
‫I'm getting an instance of the ViewModel here.
‫I'll pass it my view mode.
‫Let's go to the constructor of this add new contact handler and I'll create my ViewModel, my ViewModel.
‫Why I'm doing this?
‫Why I can't create my ViewModel directly here.
‫My ViewModel equal to new my ViewModel provider.
‫And here you can't pass this because you need an activity or fragment.
‫And this class is not extending from the appcompat or any activity.
‫So for that you need to pass the ViewModel in the constructor and then use it here.
‫You understand?
‫Guys, I want from you to focus with me in this idea.
‫I'm changing the constructor and creating my ViewModel instance just to solve this error.
‫Because when you create a My ViewModel, you need to pass inside the ViewModel provider as a ViewModel
‫store owner and this is passed from an activity or fragment exclusively.
‫So for that I remove this line and here I'll change the constructor.
‫My ViewModel, this dot, my ViewModel equal to my ViewModel.
‫Now my ViewModel is passed from add new contact activity.
‫I'll use this ViewModel in order to insert this information about the name and email of the contact
‫and create a new contact object.
‫I'll name it, see new contacts and I'll set contact dot get name and contact dot get email.
‫I need to insert this contact using ViewModel, my ViewModel dot add new contact and I'll pass.
‫See again guys.
‫When dealing with viewmodels, please pay attention to create an instance from it and pass it to the
‫classes that acts as click handlers here.
‫This class is not extending from appcompat activity, so this class is a normal class, not an activity.
‫This is an activity.
‫So inside the activity I can use ViewModel and create instances from ViewModel.
‫Let me run go to this activity and I'll name it as Richard Richard at Gmail, Click add contact to the
‫database and here we go.
‫We are directed to the main activity and Richard contact is added to our database.
‫Congratulations, guys.
‫We succeeded in inserting data into our database using an instance of my ViewModel passed to the constructor
‫of the Click Handler class.