Hi @all How can we share viewmodel between diffete...
# android
g
Hi @all How can we share viewmodel between diffetent activities ?
k
why do you want to do that?
g
I want to update my parent activity. I mean the activity from where i launch new activity
k
shouldn’t you use startActivityForResult?
g
Yes I can . But is it possible with VM?
k
Yes, you will have 2 separate view models. Activity 1 starts the second activity for result. When the activity 2 is done, it sets the result. Then activity 1 gets the result and passes it to the view model
g
Ok. So in short we can not share view model between activities like we can share between fragments. We will have to achive it using start activity for result only.
👍 1
i
You can share the viewmodel but it's not good practice. The lifecycle of the 2 activities wouldn't play well with a single viewmodel. The better option would be to startActivityForResult imo
o
I mean.. you can technically do anything... Crazy stuff like pass the ViewModel from activity1 to activity 2.... Should you do that .. like ever.... Is another question. Lol Like the gentlemen above suggested, startActivityForResult would be a better option
g
Nice. Thanks to all of you 😊