gpt4 book ai didi

android - BackHandler 'hardwareBackPress 只工作一次 | react native

转载 作者:行者123 更新时间:2023-11-29 18:40:58 26 4
gpt4 key购买 nike

我需要在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com