gpt4 book ai didi

react-native - 返回按钮 react native 退出应用程序

转载 作者:行者123 更新时间:2023-12-03 14:44:45 31 4
gpt4 key购买 nike

我在主屏幕的 native 应用程序中放置了 android 后退按钮退出应用程序功能。但是当我在其他屏幕上按下 android 后退按钮时,它也会被调用。

componentDidMount() {

if (Platform.OS == "android") {
BackHandler.addEventListener('hardwareBackPress', this.handleBackButton);
}
this._setupGoogleSignin();
this._getUserDetails();
const { navigate } = this.props.navigation;
console.log("object url is", this.state.postsArray[0].url);

}

handleBackButton = () => {
Alert.alert(
'Exit App',
'Exiting the application?', [{
text: 'Cancel',
onPress: () => console.log('Cancel Pressed'),
style: 'cancel'
}, {
text: 'OK',
onPress: () => BackHandler.exitApp()
}, ], {
cancelable: false
}
)
return true;
}
componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this.handleBackButton);
}

最佳答案

如果在导航到其他屏幕或卸载 HomeScreen 时您的 HomeScreen 仍处于安装状态如果您不删除 EventListener,它仍将被调用。

您应该在导航或卸载时清除 EventListener,

onButtonPress = () => {
BackHandler.removeEventListener('hardwareBackPress', this.handleBackButton);
// then navigate
navigate('NewScreen');
}

handleBackButton = () => {
Alert.alert(
'Exit App',
'Exiting the application?', [{
text: 'Cancel',
onPress: () = > console.log('Cancel Pressed'),
style: 'cancel'
}, {
text: 'OK',
onPress: () = > BackHandler.exitApp()
}, ], {
cancelable: false
}
)
return true;
}

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

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

关于react-native - 返回按钮 react native 退出应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46341174/

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