react-native-navigation icon indicating copy to clipboard operation
react-native-navigation copied to clipboard

[V2] StatusBar resetting its style when backing out of the app with the back button

Open denisloch opened this issue 7 years ago • 5 comments

Issue Description

I currently have one Screen and a sideMenu in my App:

Navigation.events().registerAppLaunchedListener(() => {
    Navigation.setRoot({
        root: {
            sideMenu: {
                id: "sideDrawer",
                left: {
                    component: {
                        name: "global.Drawer",
                    },
                },
                options: {
                    sideMenu: {
                        left: {
                            width: 290
                        }
                    }
                },
                center: {
                    stack: {
                        id: "centerStack",
                        children: [{
                            component: {
                                name: "component1.Main"
                            }
                        }],
                        options: `{`
                            topBar: {
                                visible: false,
                                animate: false,
                                drawBehind: true
                            }
                        }
                    },
                }
            },
        }
    });
});

I set the StatusBar color like that:

Navigation.setDefaultOptions({
    statusBar: {
        backgroundColor: "rgba(0, 0, 0, .2)",
        drawBehind: true,
        visible: true,
    }
})

Now, if I back out of my app using the back button of my phone and then open up the app again, the statusBar resets itself to the standard style (black color and drawBehind set to false).

I have no idea on how to resolve this issue, I tried pasting the second posted code snippet into both the "global.Drawer" and "component1.Main" components, but nothing seems to work and I highly assume that this is not intended.


Environment

  • React Native Navigation version: 2.4.0
  • React Native version: 0.57.8
  • Platform(s) (iOS, Android, or both?): Android
  • Device info (Simulator/Device? OS version? Debug/Release?): Android Device: OnePlus 3, Android: 8.0.0, OxygenOS: 5.0.8, Debug & Release (both)

denisloch avatar Jan 05 '19 16:01 denisloch

get solution from https://github.com/facebook/react-native/issues/13775

in MainActivity ,override invokeDefaultOnBackPressed

@Override public void invokeDefaultOnBackPressed() { moveTaskToBack(true); }

add launchMode in AndroidManifest.xml

<activity ... android:launchMode="singleTop">

add BackHandler.addEventListener in js components, and handle every backpress action,for example pop navigation:

componentDidMount(){ BackHandler.addEventListener('hardwareBackPress', this._onBackPress); }

componentWillUnmount(){ BackHandler.removeEventListener('hardwareBackPress', this._onBackPress); }

_onBackPress = () => { Navigation.pop(this.props.componentId); return true; }

asolas avatar Jan 07 '19 05:01 asolas

@asolas

Wow, thanks for bringing up this solution, using

@Override 
public void invokeDefaultOnBackPressed() {
    moveTaskToBack(true); 
}

worked like charm. I guess React Native is remounting everything, which leads to the Navigator not working as intended (or in my case: not setting the statusBar style when remounting).

I'm not so sure if that is enough to close the issue and if it even can be fixed from the react-native-navigation side or if it is a bug by react-native alone triggering a remount where there shouldn't be one.

denisloch avatar Jan 08 '19 15:01 denisloch

I was have same problems. Thanks to @asolas for pointing to solution. But solution above require adding hardwareBackPress hadler on each screen, other case back button not pop screens from stack and always put app to background. Adding handler on each screen is quite hard when there dozens of screens and complex layout. So I look into com.reactnativenavigation.NavigationActivity class, and seen there also invokeDefaultOnBackPressed method override:

  @Override
   public void invokeDefaultOnBackPressed() {
       if (!navigator.handleBack(new CommandListenerAdapter())) {
           super.onBackPressed();
       }
   }

So combining code above from react-native-navigation and:

@Override 
public void invokeDefaultOnBackPressed() {
    moveTaskToBack(true); 
}

I have added to MainActivity.java next code:

@Override
  public void invokeDefaultOnBackPressed() {
      if (!navigator.handleBack(new CommandListenerAdapter())) {
          moveTaskToBack(true);
      }
  }

Which work great for me. Hope this will help to someone.

Zaporozhec7 avatar Feb 01 '19 19:02 Zaporozhec7

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe the issue is still relevant, please test on the latest Detox and report back. Thank you for your contributions.

stale[bot] avatar Mar 03 '19 20:03 stale[bot]

The issue has been closed for inactivity.

stale[bot] avatar Mar 10 '19 20:03 stale[bot]