Nainish Modi
Nainish Modi

Reputation: 488

How can i exit from my Android App in react-native?

I am using the react-native-router-flux latest version "react-native-router-flux": "^4.0.6" and "react-native": "0.60.5". I tried many alternatives but couldn't get any success. I want to exit the native App from my home scene from physical devices or from the Emulator. Any help would be greatly appreciated.

<Router>
  <Stack key="root" hideNavBar={true}>
    <Scene key="Home" component={Home} title="Home" initial={true} type={ActionConst.RESET}/>
    <Scene key="Categories" component={Categories} title="Categories"/>
  </Stack>
</Router>

//Home.js
componentDidMount() {
    BackHandler.addEventListener('hardwareBackPress', this.handleBackButtonClick);
  }

  componentWillUnmount() {
      BackHandler.removeEventListener('hardwareBackPress', this.handleBackButtonClick);
  }

  handleBackButtonClick() {
      BackHandler.exitApp();
      return true;
  }

Upvotes: 1

Views: 215

Answers (1)

Roozbeh Mohammadzadeh
Roozbeh Mohammadzadeh

Reputation: 689

You should put this backAndroidHandler in your main Router:

with Actions.currentScene you will get the the Scene you are in it

                  <Router     
                      backAndroidHandler={() => {
                        let cs = Actions.currentScene;
                        if (cs === 'Home') {return false } 
                        else {Actions.pop()}
                       }}
                   >

Upvotes: 1

Related Questions