-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseComponent.js
38 lines (33 loc) · 1.22 KB
/
BaseComponent.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import React, {Component} from 'react';
import {
BackHandler,
Platform, ToastAndroid,
} from 'react-native';
import {NavigationEvents} from "react-navigation";
export default class BaseComponent extends Component {
constructor(props) {
super(props);
}
componentWillMount() {
this._didFocusSubscription = this.props.navigation.addListener('didFocus', () =>
BackHandler.addEventListener('hardwareBackPress', this.onBackAndroid)
);
this._willBlurSubscription = this.props.navigation.addListener('willBlur', () =>
BackHandler.removeEventListener('hardwareBackPress', this.onBackAndroid)
);
}
componentWillUnmount() {
this._didFocusSubscription && this._didFocusSubscription.remove();
this._willBlurSubscription && this._willBlurSubscription.remove();
}
onBackAndroid = () => {
if (this.lastBackPressed && this.lastBackPressed + 2000 >= Date.now()) {
//最近2秒内按过back键,可以退出应用。
BackHandler.exitApp();
return;
}
this.lastBackPressed = Date.now();
ToastAndroid.show('再按一次退出应用', ToastAndroid.SHORT);
return true;
};
}