BackHandler does not go back more than 1 screen

后端 未结 1 473
没有蜡笔的小新
没有蜡笔的小新 2021-01-12 20:17

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 b

相关标签:
1条回答
  • 2021-01-12 20:55

    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;
    }
    
    0 讨论(0)
提交回复
热议问题