Detect Hardware back button presses in Android

suggest change
BackAndroid.addEventListener('hardwareBackPress', () => {
    if (!this.onMainScreen()) {
        this.goBack();
        return true;
    }
    return false;
});

BackAndroid is the older API. Later React Native versions replace it with BackHandler.

Note: this.onMainScreen() and this.goBack() are not built in. You implement them, and the listener must see the component as this (an arrow function or .bind(this)). Returning true means your code handled the press. Returning false lets the default behavior exit the app. See https://github.com/immidi/react-native/commit/ed7e0fb31d842c63e8b8dc77ce795fac86e0f712.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents