I'm working on a Jetpack Compose app where I need to detect the device orientation (portrait or landscape) and adjust the UI accordingly.
I'm currently using LocalConfiguration to get the orientation like this:
@Composable
fun isPortraitMode(): Boolean {
return LocalConfiguration.current.orientation == Configuration.ORIENTATION_PORTRAIT
}
However, I've noticed that when I rotate the device quickly between portrait and landscape, the value from LocalConfiguration.current.orientation sometimes returns the wrong orientation or fails to update immediately.
✅ What I’ve Tried: I’ve tried adding rememberUpdatedState to update the state more consistently, but the problem persists. I also tried using LocalContext.resources.configuration.orientation, but it shows similar behavior under fast rotations.
❓ My Questions: Why is LocalConfiguration.current.orientation returning stale or incorrect values during rapid orientation changes? What’s the most reliable way to detect real-time orientation changes in Jetpack Compose?
EDIT
I have an Activity where I dynamically insert a BottomSheetDialogFragment() when needed.
In this scenario, when the device rotates, I need to update the UI of the fragment based on the new orientation. I'm currently using LocalConfiguration.current.orientation to detect the orientation and adjust the UI accordingly. However, I've noticed for the BottomSheetDialogFragment(), that after a quick rotation, the LocalConfiguration.current.orientation value often becomes outdated or returns the wrong orientation state, causing the UI update logic to fail.
What would be the most reliable way to detect and handle orientation changes in this case?