import {
BackHandler,
} from 'react-native';
componentDidMount(): void { BackHandler.addEventListener('hardwareBackPress', this.onBackButtonPressAndroid); }
onBackButtonPressAndroid = () => { if (this.props.navigation.isFocused()) { if (this.lastBackPressed && this.lastBackPressed + 2000 >= Date.now()) { //最近2秒內按過back鍵,能夠退出應用。 return false; } this.lastBackPressed = Date.now(); ToastAndroid.show('再按一次退出應用', ToastAndroid.SHORT); return true; } }
componentWillUnmount(): void { BackHandler.removeEventListener('hardwareBackPress', this.onBackButtonPressAndroid); }
不會寫,也是網上抄的,步驟分清楚,以防後期忘記掉。react