android - 'hardwareBackPress' 侦听器未在反应原生 android 中的“模糊”事件中删除
问题描述
useEffect(() => {
const backAction = () => {
Alert.alert("Hold on!", "Are you sure you want to exit the app?", [
{
text: "Cancel",
onPress: () => null,
style: "cancel"
},
{ text: "YES", onPress: () => BackHandler.exitApp() }
]);
return true;
};
const backHandler = navigation.addListener('focus', () => {
BackHandler.addEventListener('hardwareBackPress', backAction);
});
const unsubscribeblur = navigation.addListener('blur', () => {
BackHandler.removeEventListener('hardwareBackPress', _ => {
console.log('Event listener removed!');
});
});
// Return the function to unsubscribe from the event so it gets removed on unmount
return () => {
backHandler.remove();
unsubscribeblur.remove();
}
}, [navigation]);
即使添加了一个“模糊”侦听器,删除了“硬件BackPress”侦听器,警报仍然会在其他屏幕上弹出。
解决方案
模糊时您没有删除 backAction 功能。
const unsubscribeblur = navigation.addListener('blur', () => {
BackHandler.removeEventListener('hardwareBackPress', backAction) });
推荐阅读
- javascript - React 16.13:无法从子事件处理程序中不同组件的函数体内更新组件
- php - 无法检索数据 - Laravel 6
- proxy - socat 错误 - 未知选项“代理端口:8080”
- angular - Angular 9 ReactiveFormsModule - 电子邮件验证
- android - android屏幕键盘按键坐标
- python - 如何仅聚合python / pandas中列的前后值中的值
- angular - 如何在不停止排队调用的情况下使异步调用并行
- sql - postgreSQL:通过匹配行数据删除
- android - Android kotlin - 添加到数组
/ JSONArray 导致 ArrayIndexOutOfBoundsException - javascript - 根据图像分辨率和用户大小输入计算字体大小