BackHandler
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.
The event subscriptions are called in reverse order (i.e. the last registered subscription is called first).
- If one subscription returns true, then subscriptions registered earlier will not be called.
- If no subscription returns true or none are registered, it programmatically invokes the default back button functionality to exit the app.
Warning for modal users: If your app shows an opened
Modal
,
BackHandler
will not publish any events (
see Modal
docs).
BackHandler.addEventListener('hardwareBackPress', function() {
if (!this.onMainScreen()) {
this.goBack();
return true;
}
return false;
});
The following example implements a scenario where you confirm if the user wants to exit the app:
import React, { useEffect } from 'react';
import { Text, View, StyleSheet, BackHandler, Alert } from 'react-native';
export default function App() {
useEffect(() => {
const backAction = () => {
Alert.alert('Hold on!', 'Are you sure you want to go back?', [
{
text: 'Cancel',
onPress: () => null,
style: 'cancel',
},
{ text: 'YES', onPress: () => BackHandler.exitApp() },
]);
return true;
};
const backHandler = BackHandler.addEventListener('hardwareBackPress', backAction);
return () => backHandler.remove();
}, []);
return (
<View style={styles.container}>
<Text style={styles.text}>Click Back button!</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
text: {
fontSize: 18,
fontWeight: 'bold',
},
});
BackHandler.addEventListener
creates an event listener & returns a NativeEventSubscription
object which should be cleared using NativeEventSubscription.remove
method.
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 ﹣
import React, { useEffect } from 'react';
import { Text, View, StyleSheet, BackHandler, Alert } from 'react-native';
export default function App() {
useEffect(() => {
const backAction = () => {
Alert.alert('Hold on!', 'Are you sure you want to go back?', [
{
text: 'Cancel',
onPress: () => null,
style: 'cancel',
},
{ text: 'YES', onPress: () => BackHandler.exitApp() },
]);
return true;
};
const backHandler = BackHandler.addEventListener('hardwareBackPress', backAction);
return () => backHandler.remove();
}, []);
return (
<View style={styles.container}>
<Text style={styles.text}>Click Back button!</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
text: {
fontSize: 18,
fontWeight: 'bold',
},
});
React Native Hooks has a nice
useBackHandler
hook which will simplify the process of setting up event listeners.
static addEventListener(eventName, handler)
static exitApp()
static removeEventListener(eventName, handler)