使用react-native router-flux时,BackHandler无法在react-native side-menu中工作

Lav*_*aju 6 react-native react-native-android react-native-router-flux react-native-navigation

我正在研究react-native来开发一个示例应用程序.在我使用backHandlerreact-native side-menu组件时,我遇到了一个问题.

实际上,侧边菜单包含更多页面!但是当单击侧面菜单页面中的Android后退按钮时,只有后退处理程序工作.在这里,我使用react-native router-flux.

这里的后退按钮动作只调用一次!

这是我的代码:

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

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

handleBackPress = () => {
    let {isGoback} = this.props.isGoback
    //alert("Hi " + isGoback)

    if(isGoback === "Contact Us"){
        //alert("Hi: " + isGoback)
        Actions.BasicSideMenuMain({selectedItem:'Home'});
        //Actions.replace('BasicSideMenuMain')
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 0

我遇到了同样的问题,这就是我解决它的方法:当您使用 router-flux 时,您可以使用 Actions.currentScene 来查找您所在的页面

handleBackPress = () => {
        if(Actions.currentScene === 'mainPage'){   // 'mainPage' is kay of your scene
            BackHandler.exitApp();  // or anything you want
            return false;
        } 
        Actions.pop();  // for all the pages beside mainPage in side menu always go back
        return true;
    }
Run Code Online (Sandbox Code Playgroud)

希望它有效。