I am developing a trivia game, I am using react-navigation to handle navigation, I have 3 components, (newGame, Questions, Results ) I don't want the user to go back to the questions from the results page if the no. of questions has been exhausted, however, pressing the back button ( Android Hardware ) is taking him back to the questions. I then tried to handle the hardware back button like so:
componentWillMount() {
this.props.gameState(true);
BackHandler.addEventListener('hardwareBackPress', () => {
if (this.props.gamePlaying) { // Currently set to true. I will set it to false again on NewGame Page.
this.props.navigation.navigate('NewGame');
}
});
}
However, this is taking the user to the NewGame screen but immediately, it is bouncing back to the results page as it is firing the NAVIGATION/BACK as well immediately in the NewGame page. Which is again taking it back to the results page.
Possible fix?
I want to stop the the back button to fire after I have landed in the NewGame component page. Is there a way to do it?
My Environment
react-navigation = ^1.0.0-beta.11 react-native = 0.44.0
when user switch between screens in StackNavigator there is a back button by default, we can fix it by setting : headerLeft to null
You need to return
true
to indicate you have handled the back button yourself as you can see in the docs.Your code should look like this:
One way to stop the button from functioning is to introduce logic into your BackHandler eventlistener when you start it, like this:
Notice how we are observing conditions. The key to this question is to return
true
orfalse
from the event listener.false
stops the Hardware Back Button from functioning.true
clean-exits from the event.Here is another example to illustrate:
Here is some sample code that is useful to look at for more purposes than just stopping the Back Button from functioning:
If you find this post while trying to make your Back Button simply work, then just copy my last example as closely as possible. Use the event listener exactly as shown and test your app first to see how it works.
NavigationStack
simply refers toexport const NavigationStack = StackNavigator({ ...etc })
in another file.