Skip to content

Commit

Permalink
Fix hidden StatusBar reappearing after exiting app on Android
Browse files Browse the repository at this point in the history
Summary:I changed the technique used to hide the status bar and now it works properly. Also changed the way flags are set on the decorView to make sure it doesn't cause issues with other flags already set and fix deprecated Promise.reject

**Test plan**
Test using the UIExplorer StatusBar example, change the `hidden` prop value and go in and out of the app making sure the status bar has the right visibility.

Fixes #5991
Closes #6051

Differential Revision: D2960060

Pulled By: nicklockwood

fb-gh-sync-id: ee1c541896f5771d27cfd3ff18537edb6c017284
shipit-source-id: ee1c541896f5771d27cfd3ff18537edb6c017284
  • Loading branch information
janicduplessis authored and facebook-github-bot-8 committed Feb 22, 2016
1 parent b9ed72a commit cf3bd9f
Showing 1 changed file with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import android.os.Build;
import android.support.v4.view.ViewCompat;
import android.view.View;
import android.view.WindowManager;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
Expand All @@ -26,11 +27,10 @@

public class StatusBarModule extends ReactContextBaseJavaModule {

private static final String ERROR_NO_ACTIVITY =
private static final String ERROR_NO_ACTIVITY = "E_NO_ACTIVITY";
private static final String ERROR_NO_ACTIVITY_MESSAGE =
"Tried to change the status bar while not attached to an Activity";

private int mWindowFlags = 0;

public StatusBarModule(ReactApplicationContext reactContext) {
super(reactContext);
}
Expand All @@ -44,7 +44,7 @@ public String getName() {
public void setColor(final int color, final boolean animated, final Promise res) {
final Activity activity = getCurrentActivity();
if (activity == null) {
res.reject(ERROR_NO_ACTIVITY);
res.reject(ERROR_NO_ACTIVITY, ERROR_NO_ACTIVITY_MESSAGE);
return;
}

Expand Down Expand Up @@ -85,21 +85,20 @@ public void onAnimationUpdate(ValueAnimator animator) {
public void setTranslucent(final boolean translucent, final Promise res) {
final Activity activity = getCurrentActivity();
if (activity == null) {
res.reject(ERROR_NO_ACTIVITY);
res.reject(ERROR_NO_ACTIVITY, ERROR_NO_ACTIVITY_MESSAGE);
return;
}
UiThreadUtil.runOnUiThread(
new Runnable() {
@Override
public void run() {
int flags = activity.getWindow().getDecorView().getSystemUiVisibility();
if (translucent) {
mWindowFlags |=
View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
flags |= View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
} else {
mWindowFlags &=
~(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
flags &= ~(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
activity.getWindow().getDecorView().setSystemUiVisibility(mWindowFlags);
activity.getWindow().getDecorView().setSystemUiVisibility(flags);
ViewCompat.requestApplyInsets(activity.getWindow().getDecorView());
res.resolve(null);
}
Expand All @@ -111,19 +110,21 @@ public void run() {
public void setHidden(final boolean hidden, final Promise res) {
final Activity activity = getCurrentActivity();
if (activity == null) {
res.reject(ERROR_NO_ACTIVITY);
res.reject(ERROR_NO_ACTIVITY, ERROR_NO_ACTIVITY_MESSAGE);
return;
}
UiThreadUtil.runOnUiThread(
new Runnable() {
@Override
public void run() {
if (hidden) {
mWindowFlags |= View.SYSTEM_UI_FLAG_FULLSCREEN;
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
} else {
mWindowFlags &= ~View.SYSTEM_UI_FLAG_FULLSCREEN;
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
activity.getWindow().getDecorView().setSystemUiVisibility(mWindowFlags);

res.resolve(null);
}
}
Expand Down

0 comments on commit cf3bd9f

Please sign in to comment.