BackHandler

onBackAndroid = () => {
for (let i = this.handlers.length - 1; i >= 0; i--) {
if (this.handlers[i]()) {
return true;
}
}
const nav = this._navigator;
const routers = nav.getCurrentRoutes();
if (routers.length > 1) {
const top = routers[routers.length - 1];
if (top.ignoreBack || top.component.ignoreBack) {
// 路由或组件上决定这个界面忽略back键
return true;
}
const handleBack = top.handleBack || top.component.handleBack;
if (handleBack) {
// 路由或组件上决定这个界面自行处理back键
return handleBack(this.props.dispatch);
}
// 默认行为: 退出当前界面。
nav.pop();
return true;
}
else {
if (this.lastBackPressed && this.lastBackPressed + 2000 >= Date.now()) {
//最近2秒内按过back键,可以退出应用。
this.saveData();
BackHandler.exitApp();
return false;
}
this.lastBackPressed = Date.now();
Toast.info('☺再按一次就退出了哟~');
return true;
}
};

posted @ 2018-07-03 15:12  kathy+  阅读(131)  评论(0编辑  收藏  举报