32 lines
No EOL
870 B
TypeScript
32 lines
No EOL
870 B
TypeScript
import { useEffect } from 'react';
|
|
import { BackHandler, Alert } from 'react-native';
|
|
import { useRouter, usePathname } from 'expo-router';
|
|
|
|
export default function BackHandlerWrapper() {
|
|
const router = useRouter();
|
|
const pathname = usePathname();
|
|
|
|
const isHomeScreen =
|
|
!router.canGoBack() &&
|
|
(pathname.includes('home') || pathname === '/');
|
|
|
|
useEffect(() => {
|
|
const onBackPress = () => {
|
|
if (isHomeScreen) {
|
|
Alert.alert('Exit App', 'Are you sure you want to exit?', [
|
|
{ text: 'Cancel', style: 'cancel' },
|
|
{ text: 'Exit', onPress: () => BackHandler.exitApp() },
|
|
]);
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
|
|
const subscription = BackHandler.addEventListener('hardwareBackPress', onBackPress);
|
|
return () => {
|
|
subscription.remove();
|
|
};
|
|
}, [isHomeScreen]);
|
|
|
|
return null;
|
|
} |