首页 > 解决方案 > React Native Android Hardware Back 按钮无法正常工作

问题描述

登录屏幕.js

this.props.navigator.push({
      screen: "auxxa.LandingScreen",
      passProps: { login: true },
      overrideBackPress: true,
      navigatorStyle: {
        navBarHidden: true
      }
    });

LandingScreen.js

 constructor(props) {
    super(props);
    this.handleBackButtonClick = this.handleBackButtonClick.bind(this);
    //  this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
    this.state = {
      size: { width, height },
      tileData: null,
      isLoading: true,
      user_id: null,
      refetching: false,
      access_token: null
    };
  }
  componentWillMount() {
    BackHandler.addEventListener(
      "hardwareBackPress",
      this.handleBackButtonClick
    );
  }
  handleBackButtonClick() {
    console.log("check login " + this.props.login);
    if (this.backPressed && this.backPressed > 0) {
      if (this.props.login) {
        console.log("login");
        RNExitApp.exitApp();
      } else {
        console.log("root");
        this.props.navigator.popToRoot({ animated: false });
        return false;
      }
    }

    this.backPressed = 1;
    this.props.navigator.showSnackbar({
      text: "Press one more time to exit",
      duration: "long"
    });
    return true;
  }

  componentDidMount() {
    BackHandler.removeEventListener(
      "hardwareBackPress",
      this.handleBackButtonClick
    );
}

我使用Wix 的 react-native-navigation 来进行应用导航。这里我附加了登录屏幕和登录屏幕。成功登录后应用导航到登录屏幕。之后我点击返回按钮它将返回登录屏幕。我需要避免那个。我该怎么做?我试图退出应用程序。但它也无法正常工作。如果有人知道这一点,请帮助我。在此先感谢。

标签: androidreact-nativewix-react-native-navigation

解决方案


在 handleBackButtonClick 函数中使用此调用,为什么要删除 componentDidMount 中的侦听器?

this.props.navigator.resetTo({  screen: 'example.ScreenThree'})

.


推荐阅读