React Native - Device back button handling

后端 未结 9 2122
感情败类
感情败类 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 18:58

     import { BackHandler } from 'react-native';
    
     constructor() {
            super();           
            this.handleBackButtonClick = this.handleBackButtonClick.bind(this);
     }
    
       componentWillMount() {
           BackHandler.addEventListener('hardwareBackPress', this.handleBackButtonClick);
       }
    
       componentWillUnmount() {
           BackHandler.removeEventListener('hardwareBackPress', this.handleBackButtonClick);
       }
    
       handleBackButtonClick() {
           //this.props.navigation.goBack(null);
           BackHandler.exitApp();
           return true;
       }
    

提交回复
热议问题