Hey guys, I’m working with a view model in my app ...
# android-databinding
l
Hey guys, I’m working with a view model in my app and I noticed that each fragment that I use a view model provider in the data in the view model is null. But in the previous fragment I would update a field. Ex: I have a fragment that takes in an email and password and stores that into the view model. Then I have another fragment that needs to access the email and password from the view model in order to create an user account. But when I try to access it, the email and password are null
a
That can only mean that you have new instance of the view model inside second fragment. You are not getting the shared instance, but instead creating new View Model each time.
l
You're absolutely correct. I thought that was the issue too. IS there any way around this?
a
If you show me how you are getting your VM in both fragments, I could see what is wrong
Usually VM instantiated on Activity level and fragments just getting VM from activity.
l
Sorry for the late response I need to turn on my notifications but I see what you mean. I have every individual fragment creating a new view model. Instead of the main activity initializing it