BackHandler does not go back more than 1 screen

≡放荡痞女 提交于 2019-12-19 07:23:05

问题


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')
}

回答1:


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;
}


来源:https://stackoverflow.com/questions/45165323/backhandler-does-not-go-back-more-than-1-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!