Detect Hardware back button presses in Android
suggest changeBackAndroid.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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents