MENU
BackHandler
This API detects presses on the Android back button and lets you register event listeners to them.
MethodsaddEventListener()exitApp()removeEventListener() |
RESETRUNFULL
import React, { useEffect } from "react";import { Text, View, StyleSheet, BackHandler, Alert } from "react-native";const 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"}});export default App;