React Native - Device back button handling

后端 未结 9 2138
感情败类
感情败类 2020-12-02 18:33

I want to check if there are more than one screens are on stack when device back button is hit. If yes, I want to show previous screen and if no, I want to exit app.

9条回答
  •  情书的邮戳
    2020-12-02 19:06

    try this react navigation

    componentDidMount() {
            BackHandler.addEventListener('hardwareBackPress', this.handleBackButton);
        }
    
    
        handleBackButton = () => {
    
            const pushAction = StackActions.push({
                routeName: 'DefaultSelections',
            });
    
            this.props.navigation.dispatch(pushAction);
        }
    

    current screen is "DefaultSelections" , on back button press, would be shifted on to the same and hence back button disabled work around, as disabling back button by

    return true
    

    for backButton ( as suggested by the official docs ) disables back button on all screens ; not wanted

提交回复
热议问题