本文介绍了BackHandler 不会返回超过 1 个屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的每个屏幕上都有这个代码.按 android 后退按钮返回 1 个屏幕.再次按下 android 后退按钮没有任何作用.预期的结果是只要堆栈中有更多屏幕就可以继续返回.缺少什么?
I have this code on each of my screens. Pressing the android back button goes back 1 screen. Pressing android back button again does not do anything. Expected result would be to keep going back as long as there are more screens in the stack. What's missing?
componentWillMount() {
BackHandler.addEventListener('hardwareBackPress', () => {
this.props.navigation.goBack();
return true;
});
}
componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress')
}
推荐答案
经过反复试验,此代码按预期工作.我相信我的初始代码实际上并没有删除事件侦听器.
After some trial and error, this code works as expected. I believe my initial code was not actually removing the event listener.
componentWillMount() {
BackHandler.addEventListener('hardwareBackPress', this.backPressed);
}
componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this.backPressed);
}
backPressed = () => {
this.props.navigation.goBack();
return true;
}
这篇关于BackHandler 不会返回超过 1 个屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!