I have a requirement in which I need to exit the app when the back button is pressed. Basically I tried with the BackHandler
API, which worked but with certain limitations. So in my case, I feel the best way is to unmount all previously mounted components so the current screen becomes the first screen in the stack.
I have the following screens:
Login
OtpVerification
Name
Email
.
.
. and more
What I need is that when I am on the Name screen, if someone presses the back button, the app should exit. But if someone is on the Email screen, the user should be able to go back upto Name screen only.
Snippets using BackHandler
constructor(props) {
super(props);
.
.
BackHandler.addEventListener("hardwareBackPress", this._handleBackPress);
}
.
.
.
.
_handleBackPress = () => {
BackHandler.exitApp();
};
.
.
.
.
_onPress = () => {
BackHandler.removeEventListener("hardwareBackPress", this._handleBackPress);
this.props.navigation.navigate("Email", { name: this.state.name });
};