React-native-navigation: Override hardware back button

Created on 1 Jul 2018  路  9Comments  路  Source: wix/react-native-navigation

Issue Description

I want to exit my application when I pressed hardware back button, I was implemented BackHandler from react-native module, and I implement that in a modal. The event handler is worked only close active modal. How I can set to exit application when the event tirgerred?

Steps to Reproduce / Code Snippets / Screenshots

componentDidMount() {
BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
}

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

handleBackPress() {
Alert.alert('Exit', 'Are you sure?', [
{
text: 'No',
style: 'cancel'
},
{
text: 'Yes',
onPress: () => BackHandler.exitApp()
}
], {
cancelable: false
});
return true;
}

Environment

  • React Native Navigation version: ^2.0.2381
  • React Native version: 0.55.4
  • Platform(s) (iOS, Android, or both?): both
  • Device info (Simulator/Device? OS version? Debug/Release?): Simulator Android 8
馃彋 stale

Most helpful comment

Any updates on how to override the back handling in a component?

All 9 comments

Hi,
Have you solved this ?

Not yet @HRCJ7

Try this, to exit the app on exception.

    onHardwareBackPress = async () => {
        try {
            const { componentId } = this.props;
            await Navigation.pop(componentId);
            return true;
        } catch (e) {
            BackHandler.exitApp();
            return false;
        }
    };

had the same issue with LightBox
I change onBackhPressed() method.

@Override public void onBackPressed() {
        if (cancelable) {
           // hide();
        }
    }

in your case you should do the same for Modal class.

@Override
    public void onBackPressed() {
        if (!layout.onBackPressed()) {
           // super.onBackPressed();
        }
    }

Any updates on how to override the back handling in a component?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If you believe the issue is still relevant, please test on the latest Detox and report back. Thank you for your contributions.

The issue has been closed for inactivity.

any update on this?

any update ?

Was this page helpful?
0 / 5 - 0 ratings