react-native-orientation-locker icon indicating copy to clipboard operation
react-native-orientation-locker copied to clipboard

Android lockToPortrait has to be delayed in functional component

Open amwebexpert opened this issue 4 years ago • 1 comments

First: nice ❤️ library guys! I got the library set up correctly and it is working perfectly on iOS and Android. However, on android I do receive the events but the lockToPortrait doesn't work as expected and I have to do a small workaround for the orientation to get locked inside my functional components:

Does NOT work:

  useOrientationChange(console.log); // just to debug incoming events
  . . .
  useEffect(() => {
    Orientation.lockToPortrait();
  }, []);

Workaround: tried this which solves the issue on our side:

  useOrientationChange(console.log); // just to see events...
. . .
  useEffect(() => {
    setTimeout(() => Orientation.lockToPortrait(), 10); // postpone the imperative 
  }, []);

Even tried with a timeout of 0ms instead of 10ms and still fine. This timing issue suggest somehow that the native android Activity has to finish something before we call the lock api 🤔 ?

Or perhaps it's by design and I should use it differently? Just LMK, again, nice work guys!

amwebexpert avatar Sep 03 '21 11:09 amwebexpert

Having the same issue. It does not occur exactly the same on each Android device. So far we have tested on a S21 and a Pixel 5a:

Pixel 5a - Needs a timeout > 50ms (we are just using setTimeout(() => Orientation.lockToPortrait(), 100)) Galaxy S21 - Does not need a setTimeout() wrapper to lock to portrait

bachhuberdesign avatar Aug 12 '22 20:08 bachhuberdesign