How to handle exit app in one screen not in the whole app - React Native?

后端 未结 2 735
情歌与酒
情歌与酒 2021-01-05 18:23

I have some issue with BackHandler, the issue is

when running the app and go to the let\'s say Sign Up screen and touch the back in my mobile they w

2条回答
  •  孤独总比滥情好
    2021-01-05 19:04

    You can check for the screen has been focused prior to call the alert instead of checking the routeName from navigation.

    the updated code might look like this.

    if (this.props.navigation.isFocused()) {
        Alert.alert(
        "Exit App",
        "Do you want to exit?",
        [
          {
            text: "No",
            onPress: () => console.log("Cancel Pressed"),
            style: "cancel"
          },
          { text: "Yes", onPress: () => BackHandler.exitApp() }
        ],
        { cancelable: false }
        );
    }
    

提交回复
热议问题