Search code examples
androidandroid-viewmodel

How can I share a ViewModel between Activities?


I have two classes, MainActivity and RegisterActivity. Into the latter you can enter name, age, and phone number. After typing in that information I store it in a ViewModel (i.e. RegisterActivity has-a ViewModel) and click a button to finish the RegisterActivity. The thing is, I want to access the data stored in the model from within MainActivity.

So how can I share the ViewModel between the two activities? What concept do I have to use?


Solution

  • You can't share a ViewModel across Activities. That's specifically one of the downsides of using multiple activities as per the Single Activity talk.