简体   繁体   English

反应原生如何实现后退按钮

[英]react native how to implement a back button

I am not understanding how the implement a back button. 我不了解如何实现后退按钮。 The sceen is pushed programmatically and when I click on the button it will keep on popping the screen and if there are no more scene to pop then exit the react native view and return back to native view 以编程方式推动该场景,当我单击该按钮时,它将继续弹出屏幕,如果没有其他场景可以弹出,请退出react native视图并返回至native视图

Here is what I have in the activity 这是我在活动中所拥有的

    @Override
public void invokeDefaultOnBackPressed() {
    getReactInstanceManager().onBackPressed();
}

Do I need to override the OnBackPressed in this case? 在这种情况下是否需要覆盖OnBackPressed?

Here is how i setup the navigator 这是我设置导航器的方式

    render() {

    return (
        <Navigator
            initialRoute={{name: 'root'}}
            renderScene={this.renderScene.bind(this)}
            configureScene={(route) => {
                if (route.sceneConfig) {
                    return route.sceneConfig;
                }
                return Navigator.SceneConfigs.FloatFromRight;
            }} />
    );
  }
  renderScene(route, navigator) {
  return (
    <Login
      navigator={navigator} />
  );

} }

And in the login here is the first scene 在登录中是第一个场景

class Login extends Component {

constructor(props) {
    super(props);        
    _navigator = this.props.navigator
    // Private variables
    this.state = {
        isLoading: true,

    }
}


                <View style={styles.leftContainer}>
                    <TouchableHighlight style={styles.button} onPress={this.onPressButton}>
                        <Text style={[styles.buttonText]}>Invite</Text>
                    </TouchableHighlight>
                </View>

onPressButton() {
    console.log("back button");
    if (this.props.navigator && this.props.navigator.getCurrentRoutes() > 1) {
        console.log("pop");
        this.props.navigator.pop();
        //return true;
    }
    console.log("false");
    //return false;

}           

} }

When I click the button nothing happens. 当我单击按钮时,什么也没有发生。 What I want is to exit the react native view since it is at the top of the scene. 我要退出的是本机视图,因为它位于场景的顶部。

The weird thing is i see the console log printed before I even click on the button. 奇怪的是,我什至没有单击按钮就看到控制台日志已打印。 Is this normal? 这正常吗?

First of all , import BackAndroid from react-native. 首先,从react-native导入BackAndroid。 After that handle the hardware back button 之后,处理硬件后退按钮

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

If You want to close app, return false from onBackPress(), otherwise return true. 如果要关闭应用程序,请从onBackPress()返回false,否则返回true。

onBackPress(){
    this.props.nav.pop();
    return false; 
}
 onPress={this.onPressButton} 

to

onPress={() => this.onPressButton}

or 要么

onPress={this.onPressButton.bind(this)}

Bind it properly, see if that works. 正确绑定,看看是否可行。 Remember in es6 you have to properly bind your functions. 请记住,在es6中,您必须正确绑定功能。 ES5 does it automatically. ES5自动执行。

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

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