adrielcafe / voyager

🛸 A pragmatic navigation library for Jetpack Compose
https://voyager.adriel.cafe
MIT License
2.31k stars 109 forks source link

Voyager SavedStateHandle is not reset across activity manual restart #305

Open venator85 opened 4 months ago

venator85 commented 4 months ago

Hi, I'm using Voyager 1.0.0 in an Android-only project and I have encountered a strange issue with ViewModel SavedStateHandle.

In my app, when there is a session expired error, I set a specific state in a viewModel. The state is managed via SavedStateHandle to survive app process kill. When the UI detects this error state, it shows a dialog with an OK button. When the user taps the button, the activity is restarted by with finish() followed by startActivity of the same activity. The new instance of the activity should receive a new instance of the viewModel with the error state in its initial state (not set), and the dialog should not be shown.

However, after introducing Voyager in my app and managing the composable in a Screen, the error state is still set even after the activity is recreated, and the user sees the dialog again. Curiously, the error is reset after the second activity restart. This is what happens:

https://github.com/adrielcafe/voyager/assets/479928/5026130f-0cb1-4b21-88af-d9cc4d3112f3

If I manage this screen without Voyager, the issue disappears:

https://github.com/adrielcafe/voyager/assets/479928/e683bbf3-2b41-4060-9bee-ae0a016d3e06

Here you can find a project to reproduce the issue. In MainActivity, you can easily reproduce the bug / experience the correct behavior by commenting/uncommenting these lines.

Thanks for your effort!

YoussefHachicha commented 2 months ago

because you are passing the viewModel directley to your screen and that's not the way to do it