https://kotlinlang.org logo
Title
r

Remy Benza

02/07/2020, 4:11 PM
I'm using navigation components in a large complex app. It provides an extension function
by navGraphs()
which lets you scope 1 viewmodel to all the screens inside a specific navgraph. Normally you would have 1 viewmodel for each screen. Besides the benefits (easy state/data sharing between all your screens) what is a potential drawback of this approach?
d

Drew Hamilton

02/07/2020, 4:53 PM
encapsulation comes to mind – screen A can access some ViewModel function that only screen B is supposed to use?
👍 1
z

zhuinden

02/07/2020, 6:51 PM
if you don't use SavedStateHandle, then any assumptions regarding state that was set by Fragment A will be lost when you try to access it in Fragment B or Fragment C
otherwise, using a shared controller for a shared flow is a really good idea
r

Remy Benza

02/10/2020, 2:09 PM
Your answer makes sense after reading the docs. Will go for this approach.