Skip to content

Commit 331388c

Browse files
committed
Merge pull request #20 from charpeni/back-android
Add back android support
2 parents 903601c + a2e6fd3 commit 331388c

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ const firstRoute = {
5252
class MyApp extends React.Component {
5353

5454
render() {
55-
5655
return (
5756
<Router
5857
firstRoute={firstRoute}
@@ -107,6 +106,7 @@ The **`<Router \>`** object used to initialize the navigation can take the follo
107106
- `rightCorner`: If you have the same occuring action buttons on the right side of your navigation bar (like the Twitter "Compose"-button), you can specify a component for that view.
108107
- `customAction`: A special callback prop for your action buttons (this can be handy for triggering a side menu for example). The action gets triggered from your custom `leftCorner` or `rightCorner` components by calling `this.props.customAction("someActionName")` from them. It is then picked up like this: `<Router customAction={this.doSomething} />`.
109108
- `hideNavigationBar`: Hide the navigation bar, always
109+
- `handleBackAndroid` (Boolean value): Apply a listener to the native back button on Android. On click, it will go to the previous route until it reach the first scene, then it will exit the app.
110110

111111
The **`this.props.toRoute()`** callback prop takes one parameter (a JavaScript object) which can have the following keys:
112112
- `name`: The name of your route, which will be shown as the title of the navigation bar unless it is changed.

components/NavBarContainer.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { StyleSheet, View, PropTypes } from 'react-native';
1+
import React, { StyleSheet, View, PropTypes, Platform, BackAndroid } from 'react-native';
22

33
import NavBarContent from './NavBarContent';
44

@@ -8,6 +8,7 @@ const propTypes = {
88
borderColor: PropTypes.string,
99
currentRoute: PropTypes.object.isRequired,
1010
customAction: PropTypes.func,
11+
handleBackAndroid: PropTypes.bool,
1112
leftProps: PropTypes.object,
1213
navigator: PropTypes.object.isRequired,
1314
rightCorner: PropTypes.func,
@@ -50,6 +51,19 @@ class NavBarContainer extends React.Component {
5051
});
5152
}
5253

54+
componentDidMount() {
55+
if (Platform.OS === 'android' && !!this.props.handleBackAndroid) {
56+
BackAndroid.addEventListener('hardwareBackPress', () => {
57+
if (this.props.currentRoute.index > 0) {
58+
this.goBack();
59+
return true;
60+
}
61+
62+
return false;
63+
});
64+
}
65+
}
66+
5367
componentWillReceiveProps(newProps) {
5468
if (this.props && this.props.currentRoute.index !== newProps.currentRoute.index) {
5569
this.setState({
@@ -58,6 +72,12 @@ class NavBarContainer extends React.Component {
5872
}
5973
}
6074

75+
componentWillUnmount() {
76+
if (Platform.OS === 'android' && !!this.props.handleBackAndroid) {
77+
BackAndroid.removeEventListener('hardwareBackPress');
78+
}
79+
}
80+
6181
goBack() {
6282
this.props.toBack(this.props.navigator);
6383
}

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ class Router extends React.Component {
207207
rightProps={this.state.rightProps}
208208
titleProps={this.state.titleProps}
209209
customAction={this.customAction}
210+
handleBackAndroid={this.props.handleBackAndroid}
210211
/>
211212
);
212213
}

0 commit comments

Comments
 (0)