![](/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.