getStatusBarHeightPx is initialized before decorview is attached
Description
Hi everyone, after I updated the app to 0.77.3. I noticed that on Android StatusBar.currentHeight always returns 0.
Through investigation I found that at file react-native/ReactAndroid/src/main/java/com/facebook/react/modules/statusbar/StatusBarModule.kt line 48
private fun getStatusBarHeightPx(): Float { val windowInsets = currentActivity?.window?.decorView?.let(ViewCompat::getRootWindowInsets) ?: return 0f return windowInsets .getInsets( WindowInsetsCompat.Type.statusBars() or WindowInsetsCompat.Type.navigationBars() or WindowInsetsCompat.Type.displayCutout()) .top .toFloat() }
Here use decorview's getRootWindowInsets.
The problem here is that this function is called before the decorview is attached, which causes currentActivity?.window?.decorView?.let(ViewCompat::getRootWindowInsets) to always return null, and getStatusBarHeightPx()
This happens more often in release mode, but sometimes in debug mode too. It's possible that in debug mode it takes longer for metro to attach, so there's enough time for the decorview to be attached before the StatusbarHeight is initialized.
Anyone else having the same problem?
Steps to reproduce
- init project with react-native 0.77.3
- set newArchEnabled=false in gradle.properties
- try to display something StatusBar.currentHeight
- npm run android
React Native Version
0.77.3
Affected Platforms
Runtime - Android
Output of npx @react-native-community/cli info
--
Stacktrace or Logs
--
MANDATORY Reproducer
- init project with react-native 0.77.3
- set newArchEnabled=false in gradle.properties
- try to display something StatusBar.currentHeight
- npm run android
Screenshots and Videos
No response
[!WARNING] Unsupported version: It looks like your issue or the example you provided uses an unsupported version of React Native.
Due to the number of issues we receive, we're currently only accepting new issues against one of the supported versions. Please upgrade to latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If you cannot upgrade, please open your issue on StackOverflow to get further community support.
[!WARNING] Unsupported version: It looks like your issue or the example you provided uses an unsupported version of React Native.
Due to the number of issues we receive, we're currently only accepting new issues against one of the supported versions. Please upgrade to latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If you cannot upgrade, please open your issue on StackOverflow to get further community support.
[!WARNING] Missing reproducer: We could not detect a reproducible example in your issue report. Reproducers are mandatory and we can accept only one of those as a valid reproducer:
- For majority of bugs: send us a Pull Request with the RNTesterPlayground.js edited to reproduce your bug.
- If your bug is UI related: a Snack
- If your bug is build/upgrade related: a project using our Reproducer Template
You can read more about about it on our website: How to report a bug.
@kevinpvh Hey can you verify you hare facing this on new arch as well, also on latest version of react native. Unsupported version: It looks like your issue or the example you provided uses an unsupported version of React Native.
This issue is waiting for author's feedback since 24 days. Please provide the requested feedback or this will be closed in 7 days.
This issue is waiting for author's feedback since 24 days. Please provide the requested feedback or this will be closed in 7 days.
This issue is waiting for author's feedback since 24 days. Please provide the requested feedback or this will be closed in 7 days.
This issue is waiting for author's feedback since 24 days. Please provide the requested feedback or this will be closed in 7 days.
This issue is waiting for author's feedback since 24 days. Please provide the requested feedback or this will be closed in 7 days.