0

I have created a React Native App.

I am using BackHandler. https://facebook.github.io/react-native/docs/backhandler.html

I am facing one weird issue, when I am running my app using

react-native run-android

my back button is working as expected.

But when I am running using Android Studio, whenever I am clicking on back button on any screen, it is directly exiting the App.

I have checked, the bundle js file is generating properly.

Below is the code:

 import {BackHandler} from 'react-native';

    class  App extends Component {

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

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

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

    handleBackButtonClick(){
            this.props.navigation.goBack(null);
            return true;
        }
    }

EDIT: The issue is when the debugger is open, the back button is working, If not open, back button not working.

Anjana
  • 759
  • 2
  • 12
  • 33
  • @Thunder I am using the same example that is given there – Anjana Jan 08 '18 at 07:05
  • @Anjana-Systematix Can you share backhandler code – Sau_Patil Jan 08 '18 at 08:35
  • @Sau_Patil please see, I have added – Anjana Jan 08 '18 at 08:43
  • Is your App exiting due to a crash? Do you have any crash logs? – Dan Jan 08 '18 at 09:06
  • @Dan app is not exiting due to crash, no crash logs – Anjana Jan 08 '18 at 09:17
  • @Anjana-Systematix is it no crash logs are both at console log and logcat? – zainoz.zaini Jan 08 '18 at 09:59
  • yes, no crash logs @zainoz.zaini – Anjana Jan 08 '18 at 10:01
  • IMHO is do inspect of your route and state of its navigation stack.. might be it is probably because you are at the root of your app that will minimize the app window when you press the back button – zainoz.zaini Jan 08 '18 at 10:26
  • @zainoz.zaini can you pls see the edit – Anjana Jan 08 '18 at 11:40
  • @Anjana-Systematix did u mean want to run the app without JS Server?. If yes, u should understand on how RN is be packed within your published app. Here might good to read - https://www.logicroom.co/react-native-architecture-explained/ . On debugging mode after you build the package, RN-app will contact to the JS server first to pull-down the appropriate/latest JS bundle. In my case if I disconnect to the JS Server and build it back directly using AS, the app will load my VERY OLD static JS bundle. IMHO if u want to use AS to build the app,you still have to up the JS Server too. – zainoz.zaini Jan 08 '18 at 22:29
  • @zainoz.zaini thanks for the details. My issue got resolved by the code given in this link https://github.com/facebook/react-native/issues/15497 by Victoriayangx – Anjana Jan 09 '18 at 05:54

0 Answers0