I used componentDidMount
and componentDidUnmount
for android button to work. But when I come again to the same screen from another screen, the function which I have placed in android back button does not work. It works initially when I load the screen for first time but when I go to another screen and come back to that screen again the function which I have placed in back button does not work.
when I tap on the nav bar below. When I go to the other screen and come back it is working fine.. But when I tap on the nav bar and navigate to other screen and then come back again to the screen then the function does not help!! I am in a great dilemma. Particularly to make it clear: I am on the home screen, I go-to description of the post, and then come back to home screen back button function of android works..But when I tap on the nav bar, navigate to other screen and then come back clicking on the nav bar, the function does not work. –
async componentDidMount() {
BackHandler.addEventListener('hardwareBackPress',this.handleBackButtonClick);
}
componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this.handleBackButtonClick);
}
async handleBackButtonClick() {
Alert.alert(
'Exit the app?',
'Are you sure you want to exit the app?',
[
{text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
{text: 'OK', onPress: () => BackHandler.exitApp()},
],
{ cancelable: false }
)
}