2
votes

My react-native application (on Android) does not shut down when back button is pressed. It closes and goes in the background and runs there (animates).

I also tried this, but got the same outcome:

BackHandler.addEventListener('hardwareBackPress', () => {

  console.log('back pressed');
  BackHandler.exitApp();
});

I want it to completely shut down when I press back or home button.

P.S. I'm using Expo for development and deployment, and also start the app through it.

1
Seems BackAndroid.exitApp () will not kill the App's process i think - Rizal Sidik
As I see it just puts it to background - croraf
have you try to put it on componentDidMount() method ? - Rizal Sidik
No. But it get's run on back button pressed. I will now try: npmjs.com/package/react-native-exit-app. Actually I wont, you have to mess up with gradle and java stuff - croraf
i have the same problem with you, but after i put the BackHandler inside componentDidMount() method, i can solve my problem, but it's oke if you want to use that package - Rizal Sidik

1 Answers

0
votes
 constructor(props) {
                   super(props);
                   this.handleBackButtonClick = this.handleBackButtonClick.bind(this);
                 }

                 componentWillMount() {
                   BackHandler.addEventListener('hardwareBackPress', this.handleBackButtonClick);
                 }

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

                 handleBackButtonClick() {
                   BackHandler.exitApp();
                 }