简体   繁体   English

goBack在React导航中无法正常工作

[英]goBack not working properly in React Navigation

const RootNav = createStackNavigator(
  {
    Splash: {
        screen:Splash
    },
    BeforeLogin: {
        screen:BeforeLogin
    },    
    Signin: {
        screen:Signin,
    },
    Signup: {
        screen:Signup
    },
    Tabs: {
        screen:TabHolder,
    },
    ForgotPassword: {
        screen:ForgotPassword
    },
  } );

I'm using this componentWillMount in BeforeLogin screen. 我在BeforeLogin屏幕中使用了这个componentWillMount I want to close the app when user presses the android back button. 我想在用户按下android后退按钮时关闭该应用程序。

 componentWillMount() {          // In BeforeLogin Screen
   BackHandler.addEventListener('hardwareBackPress',() => {        
       BackHandler.exitApp(); 
     });  
  }

And in Signup and Signin screen, I'm using the below componentWillMount 在“ SignupSignin屏幕中,我正在使用以下componentWillMount

componentWillMount() {
    BackHandler.addEventListener('hardwareBackPress',() => {   

      this.props.navigation.goBack();
      return true;

    });
  } 

I cannot use this.props.navigation.navigate('BeforeLogin) because within Signin screen, I've a text Don't have account? 我无法使用this.props.navigation.navigate('BeforeLogin)因为在this.props.navigation.navigate('BeforeLogin)屏幕中,我有文本没有帐户? Sign up now which navigates to Signup screen. 立即注册 ,导航到“注册”屏幕。

And a Already have account? 并且已经有帐户? Sign in now on my Signup screen which navigates to Signin Screen. 现在在我的注册屏幕上登录,该屏幕导航到“ 登录屏幕”。

With all of the code I tried so far, I'm now unable to close the app when I press android back button from BeforeLogin screen. 使用到目前为止我尝试过的所有代码,当我从BeforeLogin屏幕上按下android后退按钮时,现在无法关闭该应用程序。

I tried it with goBack(null) too, it takes me to the Splash screen when I press android back button from BeforeLogin` screen. 我也尝试了goBack(null) too, it takes me to the screen when I press android back button from BeforeLogin`屏幕screen when I press android back button from goBack(null) too, it takes me to the Splash屏幕。

********************** Second alternative which also didn't work ********************** **********************第二种选择也不起作用******************** **

I used this.props.navigation.pop(1); 我使用了this.props.navigation.pop(1); in Signup and Signin Screen. 在“ SignupSignin屏幕中。 It takes me to the Splash screen when I press android back button from BeforeLogin screen. 当我从BeforeLogin屏幕上按下android后退按钮时,它将带我进入Splash屏幕。

Please help. 请帮忙。

UPDATED CODE 更新的代码

Please check the below link. 请检查以下链接。 It's the code to my question. 这是我的问题的代码。 https://gist.github.com/shubham6996/2c69f5a270d88c710ab61487639bf1af https://gist.github.com/shubham6996/2c69f5a270d88c710ab61487639bf1af

In a screen from which you want to close screen afterwards after clicking (i assume in BeforeLogin): 在您要单击后随后要关闭屏幕的屏幕中(我假设在登录之前):

constructor(props) {
    super(props);
    this._didFocusSubscription = props.navigation.addListener('didFocus', payload =>
      BackHandler.addEventListener('hardwareBackPress', this.onBackButtonPressAndroid)
    );
  }

  componentDidMount() {
    this._willBlurSubscription = this.props.navigation.addListener('willBlur', payload =>
      BackHandler.removeEventListener('hardwareBackPress', this.onBackButtonPressAndroid)
    );
  }

  onBackButtonPressAndroid = () => {
BackHandler.exitApp();
return true;
}

You cannot rely on regular lifecycles of React in React Navigation due to how stacks navigation works there (in fact they are getting some lifecycles only once as they are still mounted underneth). 由于堆栈导航在那儿的工作方式,您不能依赖React导航中常规的生命周期(实际上,由于它们仍安装在neth下,它们仅获得一些生命周期)。 So basically you need to rely on special events that are being sent by navigation itself. 因此,基本上,您需要依靠导航本身发送的特殊事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM