https://kotlinlang.org logo
#android
Title
# android
i

Ive Vasiljevic

05/30/2019, 11:09 AM
Can anyone explain what is by activityViewModels(), by fragmentViewModels(), by existingViewModels() and so on used for. I get the idea that by activityVideModels() is used for shared viewModels between mutliple fragments that need to use data from the same viewModel, but how does it work..Do I actually need to extend activity with that viewModel or what? Do I need to specifiy viewModel factory or not?
As I understood by activityViewModels() is an alternative or substitution for viewModel by lazy { ViewModelProviders.of(activity).get(ViewModel::class.java) }
l

Lilly

05/30/2019, 4:14 PM
Thats right. That also means that the viewModel has the scope of the activity so it will survive the end of every fragment. Like you said, its usually used for shared ViewModels
👍 1
358 Views