天天看點

react-navigation對android原生的傳回鍵的接管

componentWillMount(){

if (Platform.OS === 'android') {

BackHandler.addEventListener('hardwareBackPress', this._onBackAndroid);

}

}

_onBackAndroid = () => {

let {navigation} = this.props;

if (navigation.isFocused()) {

if (this.lastPressAndroidBack && this.lastPressAndroidBack + 2000 >= Date.now()) {

return false

}

this.lastPressAndroidBack = Date.now();

ToastAndroid.show('再按一次退出應用', ToastAndroid.SHORT);

return true

}

return false;

};

componentWillUnmount(){

if (Platform.OS === 'android') {

BackAndroid.removeEventListener('hardwareBackPress', this._onBackAndroid);

}

}

繼續閱讀