我需要在 Android 设备上处理 Back Press,但我的 BackHandler 只工作一次。
这是我的代码 fragment -
componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress);
}
componentDidMount() {
BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
}
handleBackPress = () => {
console.log("handleBackPress Called on RootMenuView : current View ? : " + this.state.mainView);
if (this.state.mainView === "Main"){
return false;
} else{
this.state.mainView = "Main";
this.forceUpdate();
return true;
}
}
我需要在菜单 View 上处理这个,因为我需要在其他组件的后退时显示默认组件。
最佳答案
componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress);
}
componentDidMount() {
BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
}
handleBackPress = () => {
console.log("handleBackPress Called on RootMenuView : current View ? : " + this.state.mainView);
if (this.state.mainView === "Main"){
return false;
} else{
//not this
//this.state.mainView = "Main";
// use this
this.setState({mainView: "Main"})
this.forceUpdate();
return true;
}
}
关于android - BackHandler 'hardwareBackPress 只工作一次 | react native ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53097721/