Inaccurate reporting of orientation from OrientationChangeListener in SDK39
See original GitHub issue🐛 Bug Report
Summary of Issue
ScreenOrientation OrientationChangeListener incorrectly returns orientation when changing. Change event is fired, by orientation value is same as previous value. Issue was not found in SDK 38, but present in SDK 39.
Example of working SDK 38 version in Snack. This version consistently reports correct orientation unlike SDK 39.
Environment - output of expo diagnostics
& the platform(s) you’re targeting
Expo CLI 3.28.2 environment info: System: OS: Windows 10 10.0.19041 Binaries: Node: 10.14.2 - C:\Program Files\nodejs\node.EXE npm: 6.4.1 - C:\Program Files\nodejs\npm.CMD npmPackages: expo: ~39.0.2 => 39.0.4 react: 16.13.1 => 16.13.1 react-dom: 16.13.1 => 16.13.1 react-native: https://github.com/expo/react-native/archive/sdk-39.0.4.tar.gz => 0.63.2 react-native-web: ~0.13.12 => 0.13.18 Expo Workflow: managed
Reproducible Demo
Snack currently running SDK38, issue is only present in SDK39. Reproducible with simple project created with expo init and expo-screen-orientation added via expo install.
import { StatusBar } from 'expo-status-bar';
import React, { useEffect, useState } from 'react';
import * as ScreenOrientation from 'expo-screen-orientation';
import { StyleSheet, Text, View } from 'react-native';
export default function App() {
const [screenOrientation, setScreenOrientation] = useState();
useEffect(() => {
const onOrientationChange = (currentOrientation) => {
const orientationValue = currentOrientation.orientationInfo.orientation;
setScreenOrientation(orientationValue)
}
const initScreenOrientation = async () => {
const currentOrientation = await ScreenOrientation.getOrientationAsync();
setScreenOrientation(currentOrientation);
};
const screenOrientationListener = ScreenOrientation.addOrientationChangeListener(
onOrientationChange,
);
initScreenOrientation();
return () => {
ScreenOrientation.removeOrientationChangeListener(screenOrientationListener);
};
}, []);
return (
<View style={styles.container}>
<Text>This test app shows current screen orientation as a number</Text>
<Text>{screenOrientation}</Text>
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
Steps to Reproduce
- Create new project with
expo init --template blank
- Install expo-screen-orientation with
expo install expo-screen-orientation
- Add above code to App.js
- Set orientation to default in app.json
"orientation": "default",
- Run using
expo start
on Android device - Rotate screen from portrait to landscape 10-15 times and note value of orientation shown on screen
Expected Behavior vs Actual Behavior
Expected: When rotating from Portrait to Landscape, expect change listener to get value of 3 or 4. When rotating from Landscape to Portrait, expect to get value of 1 or 2.
Actual: Sometimes getting Landscape orientation when rotating from Landscape to Portrait. Sometimes getting Portrait orientation when rotating from Portrait to landscape. Fault seems to be always due to reporting previous value rather than new orientation. Also checked with console.log directly on the event, rather than useState, same issue.
Reference enum:
export declare enum Orientation {
UNKNOWN = 0,
PORTRAIT_UP = 1,
PORTRAIT_DOWN = 2,
LANDSCAPE_LEFT = 3,
LANDSCAPE_RIGHT = 4
}
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:11 (3 by maintainers)
The issue still exists
Hi, any updates please? I am working on an app that really needs orientation to work properly, the development of multiple parts of it is stale due to this issue…