React Native android后退按钮(电话按钮)

时间:2017-12-08 04:32:42

标签: react-native react-native-navigation

我想更改或禁用Android后退按钮(手机后退按钮,而不是应用程序)。如果我用this.props.navigator.push或.resetTo加载新屏幕它工作正常但在这种情况下我需要使用this.props.navigator.showModal。

当我这样做时,它不会触发handleBackPress(console.log()确认这一点)。相反,它只是前一个屏幕,但我需要它关闭模式或禁用按钮,我不希望它转到上一个屏幕

onNavigatorEvent(event) {
    console.log("event ", event)
    try{
        switch (event.id) {
            case 'willAppear':
                this.backHandler = BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
                console.log("added")
                break;
            case 'willDisappear':
                this.backHandler.remove();
                console.log("removed")
                break;
            default:
                break;
        }
    }
    catch (ex) {
        console.log('VerifiedComponent::onNavigatorEvent: ' + ex);
    }
}

handleBackPress = () => {
    console.log("back")
    this.props.navigator.dismissAllModals({
        animationType: 'slide-down'
    })
    return true;
}

加载屏幕后,按下后​​退按钮

console.log

1 个答案:

答案 0 :(得分:0)

您应该覆盖RNN中的后退按钮:

navigator.showModal({
        screen: 'xxxx',
        overrideBackPress: true,
}

然后你可以访问支票:

if (event.id === 'backPress') {
    // do something
}