Fix status bar default on Android

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
This commit is contained in:
Arthur Lee 2019-01-10 14:25:02 -08:00 committed by Facebook Github Bot
parent 630e9faf74
commit 6c501eb666
2 changed files with 14 additions and 2 deletions

View File

@ -198,7 +198,10 @@ class StatusBar extends React.Component<Props> {
static _defaultProps = createStackEntry({ static _defaultProps = createStackEntry({
animated: false, animated: false,
showHideTransition: 'fade', showHideTransition: 'fade',
backgroundColor: 'black', backgroundColor: Platform.select({
android: StatusBarManager.DEFAULT_BACKGROUND_COLOR ?? 'black',
ios: 'black',
}),
barStyle: 'default', barStyle: 'default',
translucent: false, translucent: false,
hidden: false, hidden: false,

View File

@ -38,6 +38,7 @@ import javax.annotation.Nullable;
public class StatusBarModule extends ReactContextBaseJavaModule { public class StatusBarModule extends ReactContextBaseJavaModule {
private static final String HEIGHT_KEY = "HEIGHT"; 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 static final String NAME = "StatusBarManager";
public StatusBarModule(ReactApplicationContext reactContext) { public StatusBarModule(ReactApplicationContext reactContext) {
@ -52,14 +53,22 @@ public class StatusBarModule extends ReactContextBaseJavaModule {
@Override @Override
public @Nullable Map<String, Object> getConstants() { public @Nullable Map<String, Object> getConstants() {
final Context context = getReactApplicationContext(); final Context context = getReactApplicationContext();
final Activity activity = getCurrentActivity();
final int heightResId = context.getResources() final int heightResId = context.getResources()
.getIdentifier("status_bar_height", "dimen", "android"); .getIdentifier("status_bar_height", "dimen", "android");
final float height = heightResId > 0 ? final float height = heightResId > 0 ?
PixelUtil.toDIPFromPixel(context.getResources().getDimensionPixelSize(heightResId)) : PixelUtil.toDIPFromPixel(context.getResources().getDimensionPixelSize(heightResId)) :
0; 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( return MapBuilder.<String, Object>of(
HEIGHT_KEY, height); HEIGHT_KEY, height, DEFAULT_BACKGROUND_COLOR_KEY, statusBarColorString);
} }
@ReactMethod @ReactMethod