Landry Norris
01/04/2023, 6:00 PMvar orientation by remember { mutableStateOf(Configuration.ORIENTATION_PORTRAIT) }
val config = LocalConfiguration.current
LaunchedEffect(config) {
// Save any changes to the orientation value on the configuration object
snapshotFlow { config.orientation }
.collect { orientation = it }
}
Alex Vanyo
01/04/2023, 6:28 PMval orientation = LocalConfiguration.current.orientation
? There shouldn’t be a need to observe it separately, since LocalConfiguration.current
will update appropriately itself.
Also, I’d separately recommend double checking that the portrait vs landscape decision leads to he behavior you want. Is there a width window size class that’s more relevant? With foldables, tablets, and multi-window mode, you can see cases where your window is almost square, or where the switch from landscape to portrait might result in having more screen space to work withLandry Norris
01/04/2023, 6:36 PMLandry Norris
01/04/2023, 6:43 PMLandry Norris
01/04/2023, 6:45 PMAlex Vanyo
01/04/2023, 6:50 PMAlex Vanyo
01/04/2023, 7:19 PMConfiguration.equals
returns true
if the instance of Configuration
is the same. So the LaunchedEffect
won’t rerun when the instance stored to config
itself doesn’t change, because the old config
object instance is equal to the new config
instance, even though the value that the configuration has is different.
Then, snapshotFlow
will only output one orientation, because it is capturing that single config
instance, which doesn’t change.
LocalConfiguration
gets around that by an extremely rare use of a neverEqualPolicy
, probably for this reason, so updates to LocalConfiguration.current
will always trigger updates, even if the reference hasn’t changed.
So TL;DR: Configuration
is special, and you want to avoid the cyclic phase dependency anywayLandry Norris
01/04/2023, 7:21 PMLaunchedEffect(orientation)
instead?Alex Vanyo
01/04/2023, 7:27 PMLaunchedEffect
based on the orientation changing itself seems suspicious to me too 😄 . What does that method do?Landry Norris
01/04/2023, 7:29 PMAlex Vanyo
01/04/2023, 7:30 PMLandry Norris
01/04/2023, 7:32 PMLandry Norris
01/04/2023, 7:33 PMLandry Norris
01/04/2023, 7:35 PMAlex Vanyo
01/04/2023, 7:59 PMLandry Norris
01/04/2023, 8:00 PMAlex Vanyo
01/04/2023, 8:03 PMLandry Norris
01/04/2023, 8:06 PMAlex Vanyo
01/04/2023, 8:11 PMLandry Norris
01/04/2023, 8:13 PM