You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Detect hardware button presses for back navigation.
6
+
The Backhandler API detects hardware button presses for back navigation, lets you register event listeners for the system's back action, and lets you control how your application responds. It is Android-only.
7
7
8
-
Android: Detect hardware back button presses, and programmatically invoke the default back button functionality to exit the app if there are no listeners or if none of the listeners return true.
8
+
The event subscriptions are called in reverse order (i.e. the last registered subscription is called first).
9
9
10
-
tvOS: Detect presses of the menu button on the TV remote. (Still to be implemented: programmatically disable menu button handling functionality to exit the app if there are no listeners or if none of the listeners return true.)
10
+
-**If one subscription returns true,** then subscriptions registered earlier will not be called.
11
+
-**If no subscription returns true or none are registered,** it programmatically invokes the default back button functionality to exit the app.
11
12
12
-
iOS: Not applicable.
13
+
> **Warning for modal users:** If your app shows an opened `Modal`, `BackHandler` will not publish any events ([see `Modal` docs](modal#onrequestclose)).
13
14
14
-
The event subscriptions are called in reverse order (i.e. last registered subscription first), and if one subscription returns true then subscriptions registered earlier will not be called. Beware: If your app shows an opened `Modal`, BackHandler will not publish any events ([see `Modal` docs](modal#onrequestclose)).
this.goBack(); // works best when the goBack is async
135
+
render() {
136
+
return (
137
+
<View style={styles.container}>
138
+
<Text style={styles.text}>Click Back button!</Text>
139
+
</View>
140
+
);
141
+
}
142
+
}
143
+
144
+
const styles = StyleSheet.create({
145
+
container: {
146
+
flex: 1,
147
+
alignItems: "center",
148
+
justifyContent: "center"
149
+
},
150
+
text: {
151
+
fontSize: 18,
152
+
fontWeight: "bold"
153
+
}
154
+
});
155
+
```
156
+
157
+
<blockclass="endBlock syntax" />
158
+
159
+
`BackHandler.addEventListener` creates an event listener & returns a `NativeEventSubscription` object which should be cleared using `NativeEventSubscription.remove` method.
160
+
161
+
Additionally `BackHandler.removeEventListener` can also be used to clear the event listener. Ensure the callback has the reference to the same function used in the `addEventListener` call as shown the following example οΉ£
<Text style={styles.text}>Click Back button!</Text>
251
+
</View>
252
+
);
253
+
}
254
+
}
255
+
256
+
const styles = StyleSheet.create({
257
+
container: {
258
+
flex: 1,
259
+
alignItems: "center",
260
+
justifyContent: "center"
261
+
},
262
+
text: {
263
+
fontSize: 18,
264
+
fontWeight: "bold"
60
265
}
266
+
});
61
267
```
62
268
269
+
<blockclass="endBlock syntax" />
270
+
271
+
## Usage with React Navigation
272
+
273
+
If you are using React Navigation to navigate across different screens, you can follow their guide on [Custom Android back button behaviour](https://reactnavigation.org/docs/custom-android-back-button-handling/)
274
+
275
+
## Backhandler hook
276
+
277
+
[React Native Hooks](https://github.com/react-native-community/hooks#usebackhandler) has a nice `useBackHandler` hook which will simplify the process of setting up event listeners.
0 commit comments