![](/img/trans.png)
[英]React-Native BackHandler always closes App in Android (hardware back)
[英]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
backAction = () => {
if (this.props.navigation.isFocused() && !this.state.otpScreen) {
this.setState({ showLoginScreen: true });
return true;
} else if(this.state.showLoginScreen) {
this.props.navigation.dispatch(CommonActions.reset({
index: 0,
routes: [
{ name: 'Login' },
],
}))
return false;
}
};
上面的代码用于应该由硬件后按完成的操作,首先它应该显示一个屏幕,第二次按下它应该退出应用程序,otp 屏幕和登录屏幕都在同一个组件上,我是只是根据条件隐藏......第一次它按照条件工作,但第二次不是。
componentDidMount() {
this.focusListener = this.props.navigation.addListener('focus', () => {
this.setState({
mobile: '',
showLoginScreen: true,
});
});
this.getDataFromStorage();
AsyncStorage.setItem('countryCode', '+91');
BackHandler.addEventListener(
"hardwareBackPress",
this.backAction
);
}
componentWillUnmount() {
clearInterval(this.interval)
BackHandler.addEventListener('hardwareBackPress', () => { return false })
}
任何人都可以帮助我如何做到这一点,在此先感谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.