Handling Back Event
文章目录

React Native: Handling Back Event

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
import { View, Text, FlatList, BackHandler } from 'react-native';

componentDidMount() {
BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
}

componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress);
}

handleBackPress = () => {
console.log(123);
BackHandler.exitApp();
return true;
}

render(){
...
<Toolbar
leftElement="arrow-back"
onLeftElementPress={() => this.handleBackPress()}
centerElement="Donner Helper"
/>
...
}

上方的代码将这个方法绑定到了 Toolbar 的点击事件中

之后点击虚拟按键或者实体按键都会调用 handleBackPress() 方法