Hello how can I kill viewmodel after navigating to other screen? for example after logging out I wan...
h
Hello how can I kill viewmodel after navigating to other screen? for example after logging out I want to kill ViewModel of main screen.
s
Why do you need to kill the viewmodel, what are you trying to achieve, maybe this is a case of an XY problem? Also, why does that screen need to be in the backstack if you’re logged out? Maybe you want to navigate away from it completely and not have it in the backstack, accessible if someone just presses the back button in the first place, which would also in turn kill the VM if it wasn’t in that backstack.
c
It sounds like you’re keeping some data about the login session in the main screen’s VM, which should probably be pushed back into the app’s Repository layer
h
@Casey Brooks yes you are right. After login I was setting the state in the login page as success and navigating to main screen. In logout I am deleting the user data and navigating back to login screen. Since it's last state was success it was going back to the main screen immediately. Now to fix this in login screen I am setting state to initial before navigating to main screen. Now my question is how can I avoid this last step? I mean if the viewmodel would be destroyed I won't have to set initial state in the first place