如何使用React-Native禁用Android上的物理设备后退按钮?我不想为用户启用它。
发布于 2018-12-19 10:25:28
在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;
}
App.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-18 17:13:04
到目前为止,在v2上还没有来自React Native Navigation的开箱即用支持。但是,您可以从React本机本身使用BackHandler
。来处理它,并返回false来禁用它。
BackHandler上的文档
示例
BackHandler.addEventListener('hardwareBackPress', function() {
return false;
});
发布于 2018-12-13 17:07:55
在activity中,您可以覆盖onBackPressed()
并注释对超类的调用。
@Override
public void onBackPressed() {
// super.onBackPressed(); comment this line to disable back button press
}
https://stackoverflow.com/questions/53758271
复制相似问题