Skip to content

getStatusBarHeightPx is initialized before decorview is attached #53251

@kevinpvh

Description

@kevinpvh

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

  1. init project with react-native 0.77.3
  2. set newArchEnabled=false in gradle.properties
  3. try to display something StatusBar.currentHeight
  4. 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

  1. init project with react-native 0.77.3
  2. set newArchEnabled=false in gradle.properties
  3. try to display something StatusBar.currentHeight
  4. npm run android

Screenshots and Videos

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions