Nikhil Parab
07/01/2024, 5:39 AMStylianos Gakis
07/01/2024, 7:22 AMNikhil Parab
07/01/2024, 9:57 AMcomposable<ProblemSelectionScreen> {
val graphEntry = navController.getBackStackEntry(CreateJobCardGraph)
val viewModel: ProblemViewModel = hiltViewModel(graphEntry)
val customerDataViewModel: CustomerDataViewModel = hiltViewModel(graphEntry)
...
}
Nikhil Parab
07/01/2024, 9:58 AMCreateJobCard
is the graph, in which all the screens where I share the VMs are presentStylianos Gakis
07/01/2024, 10:07 AMCreateJobCardGraph
route then?
Or that the two viewmodels do not receive the values inside their respective SSH passed into them?Nikhil Parab
07/01/2024, 10:16 AM@Serializable
data class ProblemSelectionScreen(val customerId: Int = -1, val regNo: String = "")
agrosner
07/01/2024, 10:17 AMStylianos Gakis
07/01/2024, 10:17 AMCreateJobCardGraph
how would it receive the arguments from the specific screen? It's not scoped to that at all, I do not think it even knows about that more specific screen in that case.Stylianos Gakis
07/01/2024, 10:18 AMwhy would you need a different back stack entryLooks to me that they're scoping them to the graph in order to have the VM survive for as long as the graph is in the backstack and not the specific screens. Also to share the VMs across those destinations.
Nikhil Parab
07/01/2024, 10:21 AMStylianos Gakis
07/01/2024, 10:22 AMviewModel
lambda, which eventually tries to grab the args from the BackStackEntry which you are scoping the VM to.
The entry you are scoping it to is the graph, and not the specific destination. So it goes inside this function https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:navigati[…]y&ss=androidx%2Fplatform%2Fframeworks%2Fsupport:navigation%2F and the SSH is here https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:navigati[…]y&ss=androidx%2Fplatform%2Fframeworks%2Fsupport:navigation%2F
In both cases the args
is from the graph and not your specific screen as far as I can seeStylianos Gakis
07/01/2024, 10:25 AMBackStackEntry.toRoute()
and you can get your whole route object in a safe way too.
https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:navigati[…]java/androidx/navigation/NavBackStackEntry.kt;l=296-308;bpv=0Nikhil Parab
07/01/2024, 10:41 AMStylianos Gakis
07/01/2024, 10:43 AMval x = ssh[]....
it would anyway be null at initialization time.
If you do this inside some function instead of at initialization, have that function receive the route's args instead.Nikhil Parab
07/01/2024, 10:45 AMCustomerDataViewModel
where I am retrieving Data through API, I have in memory-cache in repo, so I was passing the userId as a key for it, I think i'll just won't share the other VM since it won't require any args, It'll work right ?Stylianos Gakis
07/01/2024, 10:45 AMStylianos Gakis
07/01/2024, 10:46 AMI have in memory-cache in repo, so I was passing the userId as a key for itYeah then sounds like one of the two VMs don't need to be alive for more than the specific screen to me. Just have it scoped there, grab that ID from the screen args and have it load things from the SSOT which is the cache. Sounds like the most robust way to do this to me.
Nikhil Parab
07/01/2024, 10:52 AMval customerDataState = customerRepository
.getNewCustomerDetails(regNo)
.map { it.toCustomerDataState() }
.stateIn(viewModelScope, SharingStarted.Lazily, CustomerDataState())
Thanks!Stylianos Gakis
07/01/2024, 10:54 AMSharingStarted.Lazily
in your StateIn
will mean that if you have this destination in your backstack, and you continue moving forward, or even leave the app and it stays alive, this flow will stay alive and querying the getNewCustomerDetails
the entire time. Even if this screen is like 10 levels behind in the backstack, it does not stop.
Quite often you want the combo of SharingStarted.WhileSubscribed()
+ collectAsStateWithLifecycle()
to avoid that.Nikhil Parab
07/01/2024, 10:55 AMStylianos Gakis
07/01/2024, 11:02 AM