[英]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.5
和react-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.