Backandler不会关闭React Native应用程序



我正在尝试触发Android上的后退按钮以关闭应用程序,但它不起作用。在JS部分,我可以看到该BackHandler.exitApp()被执行:

const handleHardwareBack = navigation => () => {
    // Back performs pop, unless we're to main screen [0,0]
    if (navigation.state.index === 0 || navigation.state.index === 2) {
        BackHandler.exitApp();
        return false;
    }
    return navigation.goBack(null);
};

在Android方面,我可以看到这部分被执行:

@Override
public void onBackPressed() {
    if (mReactInstanceManager != null) {
        mReactInstanceManager.onBackPressed();
    } else {
        super.onBackPressed();
    }
}

但是,由于总有一个MreactinstanceManager,super.onBackPressed();永远不会被拨打(这是关闭应用程序的人)。

我想念什么?如果我

,我应该/应该如何看待

尝试以下:

 import {BackHandler} from 'react-native';
export default class RoomType extends Component {
     _didFocusSubscription;
     _willBlurSubscription;
     constructor(props) {
         super(props);
         this._didFocusSubscription = props.navigation.addListener('didFocus',payload =>
            BackHandler.addEventListener('hardwareBackPress', this.onBackButtonPressAndroid)
         ); 
     }
  }
componentDidMount() {
        this._willBlurSubscription = this.props.navigation.addListener('willBlur', payload =>
            BackHandler.removeEventListener('hardwareBackPress', this.onBackButtonPressAndroid)
        );
 }
componentWillUnmount() {
        this._didFocusSubscription && this._didFocusSubscription.remove();
        this._willBlurSubscription && this._willBlurSubscription.remove();
    }
onBackButtonPressAndroid = () => {
    //code when you press the back button
    // Back performs pop, unless we're to main screen [0,0]
    if (navigation.state.index === 0 || navigation.state.index === 2) {
        BackHandler.exitApp();
        return true;
    }
 };

相关内容

  • 没有找到相关文章

最新更新