/** * Created by MeePwn * https://github.com/maybewaityou * * description: * */ import React, { PureComponent, ComponentClass, StatelessComponent } from 'react'; import { BackHandler, ToastAndroid } from 'react-native'; import { NavigationScreenProps } from 'react-navigation'; export default (WrappedComponent: ComponentClass | StatelessComponent): any => { return class extends PureComponent { public _lastBackPressed: any; public _didFocusSubscription: any; public _willBlurSubscription: any; constructor(props: any) { super(props); this._didFocusSubscription = props.navigation.addListener('didFocus', (payload: any) => BackHandler.addEventListener('hardwareBackPress', this.onBackButtonPressAndroid()), ); } public componentDidMount() { this._willBlurSubscription = this.props.navigation.addListener('willBlur', (payload: any) => BackHandler.removeEventListener('hardwareBackPress', this.onBackButtonPressAndroid()), ); } public onBackButtonPressAndroid = () => () => { const { navigation } = this.props; if (navigation.isFocused()) { if (this._lastBackPressed && this._lastBackPressed + 2000 >= Date.now()) { BackHandler.exitApp(); } this._lastBackPressed = Date.now(); ToastAndroid.show('再按一次退出应用', ToastAndroid.SHORT); return true; } else { navigation.pop(); } return true; }; public componentWillUnmount() { this._didFocusSubscription && this._didFocusSubscription.remove(); this._willBlurSubscription && this._willBlurSubscription.remove(); } public render() { return ; } }; };