使用前引入
improt {Platform,BackHandler} from react nativereact
componentDidMount() {android
if (Platform.OS === 'android'){ BackHandler.addEventListener('hardwareBackPress', this.onBackAndroid); }
}this
componentWillUnmount() {code
if (Platform.OS === 'android') { BackHandler.removeEventListener('hardwareBackPress', this.onBackAndroid); }
}component
onBackAndroid = () => {orm
//禁用返回键 if(this.props.navigation.isFocused()) {//判断 该页面是否处于聚焦状态 if (this.lastBackPressed && this.lastBackPressed + 2000 >= Date.now()) { //最近2秒内按过back键,能够退出应用。 // return false; BackHandler.exitApp();//直接退出APP }else{ this.lastBackPressed = Date.now(); ToastAndroid.show('再按一次退出应用', 1000);//提示 return true; } }
}rem