简体   繁体   English

React-Native BackHandler始终在Android中关闭App(硬件返回)

[英]React-Native BackHandler always closes App in Android (hardware back)

I'm building a App with react-native v0.44.0 using redux v5.0.5 and react-navigation v1.0.0-beta.11 . 我正在使用redux v5.0.5react-navigation v1.0.0-beta.11构建带有react-native v0.44.0的应用程序。 The routing is done with nested navigators, one main StackNavigator and DrawerNavigator. 路由是通过嵌套的导航器,一个主StackNavigator和DrawerNavigator完成的。
I'm handling all navigation events in a navigation reducer, also the hardware back press on Android using BackHandler. 我正在处理导航归约器中的所有导航事件,还使用BackHandler在Android上对硬件进行了反推。 Now comes the weird part (for me), I've implemented the BackHandler event handlers like so: 现在是怪异的部分(对我而言),我已经实现了BackHandler事件处理程序,如下所示:

import { BackHandler, Modal, View } from 'react-native';
import { NavigationActions } from 'react-navigation';    
import { HARDWARE_BACK_PRESS } from '../helpers/NavTypes';

constructor(props) {
  super(props);
  this.handleBack = this.handleBack.bind(this);
}

componentWillMount() {
  BackHandler.addEventListener(HARDWARE_BACK_PRESS, this.handleBack);
}

componentWillUnmount() {
  BackHandler.removeEventListener(HARDWARE_BACK_PRESS, this.handleBack);
}

handleBack() {
  const navAction = NavigationActions.back();

  this.props.navigation.dispatch(navAction);
  return true;
}

In my navigation reducer I'm handling the Navigation/BACK action type and keep track of my state. 在我的Navigation Reducer中,我正在处理Navigation/BACK动作类型并跟踪我的状态。 Now, when I'm pressing the hardware back button on my Android device or in the emulator, I can see thanks to redux-logger and the React Native debugger that the navigation action is dispatched correctly and the previous shown screen appears, but the app closes anyway. 现在,当我在Android设备或仿真器上按下硬件后退按钮时,由于redux-logger和React Native调试器,我可以看到导航操作已正确分派,并且出现了上一个显示的屏幕,但是该应用程序无论如何关闭。 This happens also when I alter the handleBack method to something like this: 当我将handleBack方法更改为handleBack时,也会发生这种情况:

handleBack() {
  return true;
}

Every time the hardware back button is pressed, the App still closes. 每次按下硬件后退按钮,应用程序仍然关闭。 I did some step-debugging in node_modules/react-native/Libraries/Utilities/BackHandler.android.js , inside of RCTDeviceEventEmitter.addListener I can see that my event listeners are registered and invokeDefault is set to true in the loop. 我做了一些步调试node_modules/react-native/Libraries/Utilities/BackHandler.android.js ,里面RCTDeviceEventEmitter.addListener我可以看到我的事件侦听器进行注册和invokeDefault设置为true在循环。 addListener is exited but the App still closes. addListener已退出,但该应用程序仍关闭。 Does anyone know if there is some point there react-navigation and redux are overriding the behaviour of the hardware back button at some top level I'm not aware of? 有谁知道反应和导航是否有某种意义覆盖了我不知道的某个顶级硬件后退按钮的行为?
I've setup a second plain RN project without react-navigation and redux, implementing the same BackHandler event listeners (also returning true) and the app won't close. 我已经设置了第二个没有react-navigation和redux的普通RN项目,实现了相同的BackHandler事件侦听器(也返回true),并且该应用程序不会关闭。 So, right now this leaves me a bit puzzled. 所以,现在这让我有些困惑。

I'm using react-navigation and I also handle the os back button. 我正在使用react-navigation,并且还处理了os back按钮。 It works fine for me. 这对我来说可以。 May be you can try this out. 也许您可以尝试一下。 Note that handleBack must return true if you are performing any other task than closing the app. 请注意,如果您执行除关闭应用程序以外的任何其他任务,handleBack必须返回true。 If not it will close the app immediately. 如果没有,它将立即关闭该应用程序。

 componentWillMount() {
  BackHandler.addEventListener(HARDWARE_BACK_PRESS, () => { return this.handleBack.bind(this)() });
}

I had the same problem, returning true wouldn't update. 我有同样的问题,返回true不会更新。 Running react-native run-android again after the changes fixed the problem for me. 所做的更改为我解决了问题后,再次运行react-native run-android

暂无
暂无

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

相关问题 当在 react-native 中按下硬件后退按钮时,无法对同一组件执行 2 个操作,以下是有关反处理程序的代码 - Not able to perform 2 actions on the same component when hardware back button is pressed in react-native,below are the code regarding the backhandler 如何在 React-native 中使用 Backhandler - How to use Backhandler in React-native 如何阻止Android硬件后退按钮在react-native的react-navigation中运行? - How to stop the Android Hardware Back Button from functioning in react-navigation for react-native? 使用Android硬件导航时出现导航问题Back Press [react-native] - Navigation Issue while navigating using Android Hardware Back Press [react-native] Ionic React“硬件后退按钮”关闭模式但导航回应用程序的根目录 - Ionic React "hardware backbutton" closes modal but navigates back to root of app React-Native 与 Expo:手柄硬件返回按钮 - React-Native with Expo: Handle Hardware Back Button 应用程序在react-native之前关闭之前的最后一个方法(或函数) - Last Method (or function) before app closes in react-native React Native - 带有警报的 BackHandler - React Native - BackHandler with Alert 应用程式在Android上当机(React-Native) - App crashes (React-Native) on Android 使用纯JavaScript将React-Native(Expo)应用发送到Android后退按钮按下的背景吗? - Send a React-Native (Expo) app to the background on Android back button press with pure JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM