question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

  1. Create new project with expo init --template blank
  2. Install expo-screen-orientation with expo install expo-screen-orientation
  3. Add above code to App.js
  4. Set orientation to default in app.json "orientation": "default",
  5. Run using expo start on Android device
  6. 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Balochecommented, Feb 7, 2022

The issue still exists

2reactions
CostachescuCristinelcommented, Mar 8, 2021

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…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problems when handling orientation changes - Stack Overflow
I initiate orientation switch from the Android 2.2 emulator by pressing Ctrl+F11 What could be wrong? Upd: Inner class that implements ...
Read more >
Window: orientationchange event - Web APIs | MDN
The orientationchange event is fired when the orientation of the device has changed.
Read more >
iOS 16 unexpected rotation behaviour - Apple Developer
While on iOS 15 or earlier, the app remains at Landscape and only rotates to Portrait when physical device's orientation changes.
Read more >
window.orientation incorrect if page is loaded inside of an iframe
Steps to reproduce the problem: 1. Open http://jsbin.com/kitelavo/17/ on Chrome for android 2. Alternate between landscape and portrait ...
Read more >
OrientationListener | Android Developers
Returned from onOrientationChanged when the device orientation cannot be ... value of SENSOR_DELAY_NORMAL for simple screen orientation change detection.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found