Skip to content

Commit 2d98374

Browse files
committed
Add back android support
1 parent e9c95b9 commit 2d98374

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ class MyApp extends React.Component {
5252
}
5353

5454
render() {
55-
return <Router
55+
return <Router
5656
firstRoute={firstRoute}
57-
headerStyle={this.styles.header}
57+
headerStyle={this.styles.header}
5858
/>
5959
}
6060
}
@@ -104,6 +104,7 @@ The **`<Router \>`** object used to initialize the navigation can take the follo
104104
- `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.
105105
- `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} />`.
106106
- `hideNavigationBar`: Hide the navigation bar, always
107+
- `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.
107108

108109
The **`this.props.toRoute()`** callback prop takes one parameter (a JavaScript object) which can have the following keys:
109110
- `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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const {
66
StyleSheet,
77
View,
88
Component,
9+
Platform,
10+
BackAndroid,
911
} = React;
1012

1113
class NavBarContainer extends Component {
@@ -22,6 +24,19 @@ class NavBarContainer extends Component {
2224
};
2325
}
2426

27+
componentDidMount() {
28+
if (Platform.OS === 'android' && !!this.props.handleBackAndroid) {
29+
BackAndroid.addEventListener('hardwareBackPress', () => {
30+
if (this.props.currentRoute.index > 0) {
31+
this.goBack();
32+
return true;
33+
}
34+
35+
return false;
36+
});
37+
}
38+
}
39+
2540
componentWillReceiveProps(newProps) {
2641
if (this.props && this.props.currentRoute.index !== newProps.currentRoute.index) {
2742
this.setState({
@@ -30,6 +45,12 @@ class NavBarContainer extends Component {
3045
}
3146
}
3247

48+
componentWillUnmount() {
49+
if (Platform.OS === 'android' && !!this.props.handleBackAndroid) {
50+
BackAndroid.removeEventListener('hardwareBackPress');
51+
}
52+
}
53+
3354
goBack() {
3455
this.props.toBack(this.props.navigator);
3556
}

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ class Router extends React.Component {
185185
rightProps={this.state.rightProps}
186186
titleProps={this.state.titleProps}
187187
customAction={this.customAction}
188+
handleBackAndroid={this.props.handleBackAndroid}
188189
/>
189190
);
190191
}

0 commit comments

Comments
 (0)