BackHandler does not return more than 1 screen

I have this code on each of my screens. Pressing the button backward backward 1 screen. Pressing the back button backward does nothing. The expected result would be to keep coming back while there are more screens in the stack. What is missing?

componentWillMount() {
    BackHandler.addEventListener('hardwareBackPress', () => {
        this.props.navigation.goBack();
        return true;
    });
}

componentWillUnmount() {
  BackHandler.removeEventListener('hardwareBackPress')
}
+4
source share
1 answer

After some trial and error, this code works as expected. I believe that my original code did not actually remove the event listener.

componentWillMount() {
    BackHandler.addEventListener('hardwareBackPress', this.backPressed);
}

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

backPressed = () => {
    this.props.navigation.goBack();
    return true;
}
+5
source

Source: https://habr.com/ru/post/1681689/


All Articles