Skip to content

Commit 0688b9b

Browse files
jainkuniyaborisyankov
authored andcommitted
Handle Android hardware back button press. (#1125)
* Handle Android hardware back button press. Navigate back onPress. * fix: flow errors.
1 parent 6594f8e commit 0688b9b

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/nav/AppContainer.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
/* @flow */
22
import React, { PureComponent } from 'react';
3-
import { AppState, NetInfo, View } from 'react-native';
3+
import { AppState, BackHandler, NetInfo, View } from 'react-native';
44
import { connect } from 'react-redux';
55

66
import boundActions from '../boundActions';
77
import AppWithNavigationState from './AppWithNavigationState';
8-
import { getAuth } from '../selectors';
8+
import { getAuth, getNavigationIndex } from '../selectors';
99
import { registerAppActivity } from '../utils/activity';
1010
import { checkCompatibility } from '../api';
1111
import CompatibilityScreen from '../start/CompatibilityScreen';
1212
import { Auth, Actions } from '../types';
1313

1414
type Props = {
1515
auth: Auth,
16+
navIndex: number,
1617
needsInitialFetch: boolean,
1718
actions: Actions,
1819
};
@@ -48,6 +49,15 @@ class AppContainer extends PureComponent {
4849
actions.appState(state === 'active');
4950
};
5051

52+
handleBackButtonPress = () => {
53+
const { navIndex, actions } = this.props;
54+
if (navIndex !== 0) {
55+
actions.navigateBack();
56+
return true;
57+
}
58+
return false;
59+
}
60+
5161
handleMemoryWarning = () => {
5262
// Release memory here
5363
};
@@ -56,6 +66,7 @@ class AppContainer extends PureComponent {
5666
NetInfo.isConnected.addEventListener('change', this.handleConnectivityChange);
5767
AppState.addEventListener('change', this.handleAppStateChange);
5868
AppState.addEventListener('memoryWarning', this.handleMemoryWarning);
69+
BackHandler.addEventListener('hardwareBackPress', this.handleBackButtonPress);
5970
checkCompatibility().then(res => {
6071
if (res.status === 400) {
6172
this.setState({
@@ -102,6 +113,7 @@ export default connect(
102113
state => ({
103114
auth: getAuth(state),
104115
needsInitialFetch: state.app.needsInitialFetch,
116+
navIndex: getNavigationIndex(state),
105117
}),
106118
boundActions,
107119
)(AppContainer);

0 commit comments

Comments
 (0)