监听rn中物理返回键,在禁止返回上一页,连续点击两下退出应用,
设置常量
let lastBackPressed = Date.now()
componentWillMount() {this.getCallLog(); if (Platform.OS === ‘android‘) { BackHandler.addEventListener(‘hardwareBackPress‘, this.onBackAndroid); } }
componentWillUnmount() {if (Platform.OS === ‘android‘) { // this.showToast(‘销毁‘); BackHandler.removeEventListener(‘hardwareBackPress‘, this.onBackAndroid); } }
onBackAndroid() { if (lastBackPressed && lastBackPressed + 2000 >= Date.now()) { //最近2秒内按过back键,可以退出应用。 BackHandler.exitApp(); return; } lastBackPressed = Date.now(); ToastAndroid.show(‘再按一次退出应用‘,ToastAndroid.SHORT); return true; }