且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在一个屏幕中处理退出应用程序而不是整个应用程序 - React Native?

更新时间:2023-01-09 23:38:11

您可以检查之前已经关注的屏幕调用警报而不是从导航中检查 routeName

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 }
    );
}