hi guys, The viewmodel is cleared when popbackstac...
# compose
t
hi guys, The viewmodel is cleared when popbackstack is called or when I press the back button . But when I come back to the screen it doesn’t start again. Is there a proper solution to this?
1
i
That sounds like everything is working as intended: the ViewModel is supposed to exist for the entire time that entry exists on the navigation stack
t
Yes I know that and I understand why. But if the user presses the back button on each view model screen, the viewmodel will terminate. Do I need to handle the back button on each page one by one for this?
i
Pressing the system back button removes that screen from the back stack, destroying all state (including ViewModels) associated with that screen. That also sounds like everything is working as intended.
o
or do you mean you go A -> B, back press on B, and then you find viewmodel destroyed for A?
t
I mean, I am in A and i go B then i press back button it is going to A normally. Then i going to B again and viewModel not working. A (navigate) -> B (back press) -> A (navigate) -> B
In fact, if you press the back button in a scene, that scene disappears with everything in it. This is what it should be. My problem is when I navigate to that scene the viewmodel doesn’t work again.
s
The ViewModel doesn't "work" is quite a sentence. What is it that doesn't "work"?
o
once you press back on B, the viewmodel will be destroyed since its lifecycle that is linked with the view is destroyed, when you go back to B the vm will start from scratch, which is what’s expected not sure what your use case is where you need B to be alive even after leaving it
s
What's the code like inside that ViewModel? Also how do you call it from your composable?
i
You need to remove that whole
AppModule
. ViewModels are not singletons and you don't use
@Provides
with them
s
☝️ I am curious now, since he did do that mistake, did this behavior happen due to the fact that the ViewModel was cleared and it's viewModelScope was cancelled from that point on, resulting in all
.launch
calls on it doing nothing? Asking just to satisfy my curiosity 😅
i
Yep, as part of
onCleared()
, the
viewModelScope
is cancelled, so any
launch
isn't going to do anything. I suspect that LeakCanary would also be showing a leak for continuing to hold onto a ViewModel once it is cleared (it is absolutely a leak at that point)
s
Makes total sense then! Taryel if you'd want to confirm this you can do a log before the viewModelScope launch to check if that scope is cancelled or not. It will show that it's cancelled and it will then make sense why it "stopped working"
t
Actually, I totally understand what you’re saying. And it’s very logical. But if I use useCase as an example, I don’t know how to inject it into the viewmodel. Is there a sample repo I can look into?
i
If you want to provide something at the ViewModel level, that sounds like something you'd use
ViewModelScoped
for: https://medium.com/androiddevelopers/using-hilts-viewmodelcomponent-53b46515c4f4
t
I’ve always been wrong up until now. Thank you very much. It helped a lot.
386 Views