[RN] React Native 再按一次退出

react

实现 React Native 再按一次退出

单页面:

... 

componentWillMount() {

BackHandler.addEventListener('hardwareBackPress', this.onBackAndroid)

}

lastBackPressed = 0

onBackAndroid = () => {

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

BackHandler.exitApp()

return false

}

this.lastBackPressed = Date.now()

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

return true

}

componentWillUnmount() {

BackHandler.removeEventListener('hardwareBackPress', this.onBackAndroid)

}

...

包含在导航器中:

let lastBackPressed = 0;

const defaultStateAction = RootNav.router.getStateForAction;

RootNav.router.getStateForAction = (action, state) => {

if (DEVICE.android && state && action.type === NavigationActions.BACK && state.routes.length === 1) {

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

BackHandler.exitApp();

return false;

}

this.lastBackPressed = Date.now();

ToastUtil.showToast("再按一次退出应用");

const routes = [...state.routes];

return {

...state,

...state.routes,

index: routes.length - 1,

};

} else {

return defaultStateAction(action, state);

}

};

本博客地址: wukong1688

本文原文地址:https://www.cnblogs.com/wukong1688/p/10959609.html

转载请著名出处!谢谢~~

以上是 [RN] React Native 再按一次退出 的全部内容, 来源链接: utcz.com/z/382536.html

回到顶部