简体   繁体   English

覆盖后退按钮 wix 反应原生导航 V2?

[英]Override back button wix react native navigation V2?

I am currently transitioning from Wix RNN V1 to V2, and so far I've managed to find the appropriate replacement APIs, except for overriding the back button on Android.我目前正在从 Wix RNN V1 过渡到 V2,到目前为止,我已经设法找到合适的替代 API,除了覆盖 Android 上的后退按钮。

In V1 we could pass the overrideBackPress: true attribute, and then handle back button presses manually on the cooresponding screen.在 V1 中,我们可以传递overrideBackPress: true属性,然后在相应的屏幕上手动处理后退按钮按下。

However, in V2 I've found no such replacement, and the only topics I could find were this thread:但是,在 V2 中我没有找到这样的替代品,我能找到的唯一主题是这个线程:

https://github.com/wix/react-native-navigation/issues/4217 https://github.com/wix/react-native-navigation/issues/4217

I've implemented the suggestions there, but Wix navigation is still automatically closing screens even though it should be overwritten.我已经在那里实施了建议,但是即使应该覆盖 Wix 导航,它仍然会自动关闭屏幕。

Any known a solution for this?任何已知的解决方案?

I had the same issue and the only way i could override the backpress behavior on both platforms is to replace the left back button with custom button and use the BackHandler of react native for the hardware button in Android.我遇到了同样的问题,我可以在两个平台上覆盖 backpress 行为的唯一方法是用自定义按钮替换左后退按钮,并为 Android 中的硬件按钮使用 React Native 的 BackHandler。 The code is as below.代码如下。

Component A组分 A

//Navigate to component B from A
Navigation.push(this.props.componentId, {
component: {
    name: 'ComponentB',
    options: {
        topBar: {
            leftButtons: [{
                id: 'backPress',
                text: 'Back',
                icon: require('backbutton.png')
            }]
        },
    }
}
});

Component B组分 B

import React, { PureComponent } from 'react';
import { View, BackHandler } from 'react-native';
import { Navigation } from 'react-native-navigation';

export default class ComponentB extends PureComponent {

constructor(props) {
    super(props);
    Navigation.events().bindComponent(this);
}

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

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

navigationButtonPressed({ buttonId }) {
    switch (buttonId) {
        case 'backPress': {
            this.handleBackPress();
            break;
        }
    }
}

handleBackPress = () => {
    //Custom logic

    //Go back if required
    Navigation.pop(this.props.componentId)

    //Stop the default navigation
    return true;
};

//Render component
render() {
    return (<View></View>);
}
}

You can use registerScreenPoppedListener :您可以使用registerScreenPoppedListener

Navigation.events().registerScreenPoppedListener((event) => {
    if (event.componentId === "my-screen-id") {
        // do something
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM