4

如何使用 React-Native 在 Android 上禁用物理设备后退按钮?我不想为用户启用它。

4

3 回答 3

5

到目前为止,React Native Navigation 在 v2 上没有开箱即用的支持。但是,您可以BackHandler从 React native 本身使用。处理它并返回 false 以禁用它。

BackHandler上的文档

例子

BackHandler.addEventListener('hardwareBackPress', function() {
  return false;
});
于 2018-12-18T09:13:03.620 回答
1

在活动中,您可以覆盖onBackPressed()并评论对超类的调用。

@Override
public void onBackPressed() {
  // super.onBackPressed(); comment this line to disable back button  press
}
于 2018-12-13T09:07:55.387 回答
1

在 app.js 中使用启动应用程序时创建的侦听器来跟踪当前打开的屏幕。应用的主要组件创建一个 BackHandler 监听器,它根据当前打开的屏幕响应设备返回按钮。

主要成分:

componentDidMount() {
  BackHandler.addEventListener('hardwareBackPress', this.onBackPress);
}

componentWillUnmount() {
  BackHandler.removeEventListener('hardwareBackPress', this.onBackPress);
}

onBackPress = () => {
  if (this.props.currentScreen.name === 'app.main') {
     Alert.alert(
       'Confirm exit',
       'Do you want to exit App?',
       [
         {text: 'CANCEL', style: 'cancel'},
         {text: 'OK', onPress: () => {
           BackHandler.exitApp()
          }
        }
       ]
    );
  } else {
    Navigation.pop(this.props.currentScreen.id);
  }

  return true;
}

应用程序.js

//register to compomentDidApperaListener to keep track on which screen is currently open. The componentName and Id are stored in my redux store
Navigation.events().registerComponentDidAppearListener(({ componentId, componentName }) => {
  store.dispatch(updateCurrentScreen({'name': componentName, 'id': componentId}))
})
于 2018-12-19T02:25:27.970 回答