Pass viewmodel instance to another class and call viewmodel.update(), viewmodel.insert() and other functions from that class

Published

I create a ViewModel in my MainActivity.java:

AnchorViewModel anchorViewModel = new ViewModelProvider(this).get(AnchorViewModel.class);
        anchorViewModel.getAllAnchors()
                .observe(this, updateArObjectsOnChangeInSceneHelper::updateAnchorsInArScene);

Then, I pass the anchorViewModel to another class. Now I want to call anchorViewModel.insert() and anchorViewModel.update() from that class instead of MainActivity.java. I try that but it doesn’t trigger the updateAnchorsInArScene function. In fact, I can only trigger it when I call the anchorViewModel methods from the MainActivity.java. Am I not allowed to call ViewModel methods from a class different than the one where it was created (even if I just pass it as a reference to that other class)?

Source: Android Questions

Published
Categorized as android, android-mvvm, android-viewmodel, java

Answers

Leave a Reply

Still Have Questions?


Our dedicated development team is here for you!

We can help you find answers to your question for as low as 5$.

Contact Us
faq