美文网首页
react-native 连续两次点击后退退出应用

react-native 连续两次点击后退退出应用

作者: 暗木幽浅 | 来源:发表于2017-11-09 14:11 被阅读0次
import {
    BackHandler,
    ToastAndroid
} from 'react-native'

let lastTime = 0

let handler = null

const bindBackExitApp = () => {
    handler = BackHandler.addEventListener('hardwareBackPress', () => {
        let time = Date.now()
        if (time - lastTime <= 2000) {
            BackHandler.exitApp()
            return false
        } else {
            ToastAndroid.show('再按一次退出应用', ToastAndroid.SHORT)
            lastTime = time
            return true
        }
    })
}

const removeBackExitApp = () => {
    handler && handler.remove()
}

export {
    bindBackExitApp,
    removeBackExitApp
}

相关文章

网友评论

      本文标题:react-native 连续两次点击后退退出应用

      本文链接:https://www.haomeiwen.com/subject/dbipmxtx.html