Backhandler does not work

Asked

Viewed 21 times

0

In my app React-Native PWA the back button of Android is closing the application. I tried to use this Backhandler function inside any screen to make it come back instead of closing the app but it doesn’t work. Someone’s been through it and has a solution?

  useFocusEffect(useCallback(() => { 
    const onBackPress = () => {
      goBack();
      return true
    };

    BackHandler.addEventListener('hardwareBackPress', onBackPress);

    return () =>
      BackHandler.removeEventListener('hardwareBackPress', onBackPress);
  }, []));

Does not work in PWA.

  • 1

    Welcome to [en.so]. Here the content is in Portuguese, but you can ask your question in English in [so]. About your question is there any screen in the "stack" navigation? Because if there is not, Android actually exits the application as default behavior

  • There is a stack created with createStackNavigator. Routes work normal and come back normal with the header back button in PWA. The problem is only the back button of the same android. It closes the app both in PWA installed as using only in the browser

2 answers

0

0

You can abstract into something similar:

const onBackPress = useCallback(
    () => {
      if(isFocused){
        navigation.goBack();
        return true
      }
      return false
    }, [isFocused],
  )

  useEffect(() => {
    BackHandler.addEventListener('hardwareBackPress', onBackPress);
    return () => BackHandler.removeEventListener('hardwareBackPress', onBackPress);
  }, [])


Browser other questions tagged

You are not signed in. Login or sign up in order to post.