android-back-button
is a React Native module for hardware back button on Android devices. Component listens for press event and provides developer with simple way to manage it. Fully compatible with iOS.
Via npm
:
npm install android-back-button --save
Via yarn
:
yarn add android-back-button
Simply add AndroidBackButton
component to scene's layout:
import React, {
Component
} from 'react';
import {
View
} from 'react-native';
import AndroidBackButton from 'android-back-button';
export default class Scene extends Component {
render() {
const {navigator} = this.props;
return (
<View style={styles.container}>
<AndroidBackButton
onPress={() => {
navigator.pop();
return true;
}}
/>
</View>
);
}
}
As you can see in example above, AndroidBackButton
component uses onPress
property which is a handler for press event on Android hardware back button.
onPress
method should return boolean value:
-
false
if you want to close app; -
true
if you want to continue using app.
You can implement any custom behavior inside onPress
handler which gives you a super-flexible approach for managing navigation inside of app.
android-back-button
is available under the MIT license. See the LICENSE file for more info.