claudiudbc
claudiudbc

Reputation: 31

Go back from remote "menu" button on tvOS (Apple TV) with React Native

I need to implement back from remote "menu" button on tvOS (Apple TV) and the BackHandler from React Native doesn't work. I understand that I have to use TVMenuControl and disable the TV menu key and go back, but this doesn't work even if the docs says it should work.

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;

};

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

How can I do this?

Thanks in advance, Claudiu

Upvotes: 2

Views: 1742

Answers (1)

Gabriel Rodriguez
Gabriel Rodriguez

Reputation: 41

you need to enable the menu button functionality to make your code works

import {  TVMenuControl, BackHandler } from 'react-native';
....
useEffect(() => {
      TVMenuControl.enableTVMenuKey();
      BackHandler.addEventListener("hardwareBackPress", backAction);
    return () => {
     TVMenuControl.disableTVMenuKey();
      BackHandler.removeEventListener("hardwareBackPress", backAction);
    }
    }, []);

Upvotes: 4

Related Questions