孙肖宁

React Native安卓连续按返回两次之后退出
1.在componentWillMount中 componentWillMount() { thi...
扫描右侧二维码阅读全文
21
2018/11

React Native安卓连续按返回两次之后退出

1.在componentWillMount

 componentWillMount() {
        this.backHandler && this.backHandler.remove();
    }

2.在componentDidMount

componentDidMount() {
    this.backHandler = BackHandler.addEventListener('hardwareBackPress',
        this.onBackButtonPressAndroid);
}

3.退出函数

  onBackButtonPressAndroid = () => {
        if (this.props.navigation.isFocused()) {
            if (this.lastBackPressed && this.lastBackPressed + 2000 >= Date.now()) {
                //最近2秒内按过back键,可以退出应用。
                return false;
            }
            this.lastBackPressed = Date.now();
            NRJCore.toast("再按一次退出");
            return true;
        }

    }
Last modification:November 26th, 2018 at 02:52 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment