最近在做一个跨平台的项目,刚用Tab-Navigator做完首页底部导航菜单,发现个问题,Android返回键一按下,app就退出了,这不符合app的玩法啊,各种找解决方案,终于实现了.暂时先做个记录,后面在详细完善下全局的返回键处理
var firstClick = 0;
export default class MainActivity extends React.Component {
constructor(props) {
super(props);
this.state = {
selectedTab: 'home',
}
this.handleBack = this.handleBack.bind(this);
}
componentDidMount() {
BackHandler.addEventListener('hardwareBackPress', this.handleBack)
}
componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this.handleBack)
}
handleBack = () => {
var timestamp = (new Date()).valueOf();
if (timestamp - firstClick > 2000) {
firstClick = timestamp;
ToastAndroid.show('再按一次退出', ToastAndroid.SHORT);
return true;
} else {
return false;
}
}