Android lockToPortrait has to be delayed in functional component
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!
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