Assuming you're using Navigator
your index.android.js
file should look like:
export default class Foo extends React.Component {
constructor(props) {
super(props);
this._navigator = null;
}
renderScene(route, navigator) {
if(!this._navigator){
this._navigator = navigator;
BackAndroid.addEventListener('hardwareBackPress', () => {
if (this._navigator && this._navigator.getCurrentRoutes().length > 1) {
this._navigator.pop();
return true;
}
return true;
});
}
return React.createElement(component, { ...this.props, ...route.passProps, route, navigator } );
}
render() {
return (
<Navigator
initialRoute={...}
configureScene={...}
renderScene={(route, nav) => {return this.renderScene(route, nav)}}
/>
);
}
}
AppRegistry.registerComponent('Foo', () => Foo);
If you're not using Navigator
IDK how you can accomplish this because hardwareBackPress
doesn't know anything about the position on the stack.