How to listen to switch app button in Android React Native?

Viewed 30

how I could listen when the user press this button in React-Native? If is not possible how could I listen in java?

enter image description here

I know that in React-Native we could listen if user press the home button and back button using "BackHandler", something like this:

    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;
0 Answers
Related