Skip to content

Commit

Permalink
Fix status bar default on Android
Browse files Browse the repository at this point in the history
Summary: On Android, the status bar color is not always black by default. The existing code causes the status bar to revert to black once the last `<StatusBar>` component is unmounted from the "stack". This diff reverts the bar background color to what it was before, instead of assuming black.

Reviewed By: yungsters

Differential Revision: D13368136

fbshipit-source-id: ef0154f776607b57bb9400b72d521f5f485b0075
  • Loading branch information
Arthur Lee authored and facebook-github-bot committed Jan 10, 2019
1 parent 630e9fa commit 6c501eb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Libraries/Components/StatusBar/StatusBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ class StatusBar extends React.Component<Props> {
static _defaultProps = createStackEntry({
animated: false,
showHideTransition: 'fade',
backgroundColor: 'black',
backgroundColor: Platform.select({
android: StatusBarManager.DEFAULT_BACKGROUND_COLOR ?? 'black',
ios: 'black',
}),
barStyle: 'default',
translucent: false,
hidden: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
public class StatusBarModule extends ReactContextBaseJavaModule {

private static final String HEIGHT_KEY = "HEIGHT";
private static final String DEFAULT_BACKGROUND_COLOR_KEY = "DEFAULT_BACKGROUND_COLOR";
public static final String NAME = "StatusBarManager";

public StatusBarModule(ReactApplicationContext reactContext) {
Expand All @@ -52,14 +53,22 @@ public String getName() {
@Override
public @Nullable Map<String, Object> getConstants() {
final Context context = getReactApplicationContext();
final Activity activity = getCurrentActivity();

final int heightResId = context.getResources()
.getIdentifier("status_bar_height", "dimen", "android");
final float height = heightResId > 0 ?
PixelUtil.toDIPFromPixel(context.getResources().getDimensionPixelSize(heightResId)) :
0;
String statusBarColorString = "black";

if (activity != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
final int statusBarColor = activity.getWindow().getStatusBarColor();
statusBarColorString = String.format("#%06X", (0xFFFFFF & statusBarColor));
}

return MapBuilder.<String, Object>of(
HEIGHT_KEY, height);
HEIGHT_KEY, height, DEFAULT_BACKGROUND_COLOR_KEY, statusBarColorString);
}

@ReactMethod
Expand Down

0 comments on commit 6c501eb

Please sign in to comment.