BackAndroid不会退出应用程序

BackAndroid does not quit application

本文关键字:应用程序 退出 BackAndroid      更新时间:2023-09-26

我在 react-native android 中使用 BackAndroid。当我从主页移动到下一页并返回主页时,它工作正常,但是如果我按回去,它不会退出应用程序。

这是我的代码:

componentWillMount: function(){
    let context = this
    if (Platform.OS !== 'ios') {
        BackAndroid.addEventListener('hardwareBackPress', context.backAndroidHandler);
    }
  },
  componentWillUnmount: function(){
    let context = this
    var Meteor = context.props.value.DDPClient;
    // TODO Unsuscribe is not a valid function
    Meteor.unsuscribe(subscriptionId);
    context.props.value.setCurrentConversationID(null);
    if (Platform.OS !== 'ios') {
        BackAndroid.removeEventListener('hardwareBackPress', context.backAndroidHandler);
      }      
  },
  backAndroidHandler: function(){
      // console.log('hardwareBackPress');
      this.props.navigator.pop();
      return true;
  },

您指定按钮应该弹出(),无论堆栈视图是什么。您可以在backAndroidHandler中做的是:

if (this.props.navigator.getCurrentRoutes().length > 1) {
  this.props.navigator.pop();
  return true;
} else {
  return false;
}