adrielcafe / voyager

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

By default screens save its state when it is unexpected #105

Open terrakok opened 1 year ago

terrakok commented 1 year ago

At the moment default screen key is this::class.qualifiedName. It leads to a behavior when new screen restores a previous state because it was saved in the past.

A typical scenario is: Login screen -[replace]-> Main screen -[replace]-> Login Screen

Last Login screen will restore state from the first launch because it depends on its key.

Better solution is to generate random key for each usage

Syer10 commented 1 year ago

See uniqueScreenKey from https://voyager.adriel.cafe/state-restoration#identifying-screens

terrakok commented 1 year ago

Thank you. I know the uniqueScreenKey function. I said about default behavior

Syer10 commented 1 year ago

It can't be default beavior since Screen is an interface, and interfaces cannot have backing fields. I believe the AndroidScreen from the androidx module uses uniqueScreenKey by default since its an abstract class. You could use a similar solution if you don't use the AndroidScreen

terrakok commented 1 year ago
image
terrakok commented 1 year ago

Oh! I see :) we cannot save value here

terrakok commented 1 year ago

but, anyway, default behavior is quite confusing because it hides the problem. maybe lack of default key would be better