Skip to content

Commit

Permalink
Feat(BackAndroid): Add handler for Back Key in Android (#1049)
Browse files Browse the repository at this point in the history
* Feat(TabBar): add pressOpacity for TabBar

* Feat(Doc): add doc for pressOpacity

* Feat(BackAndroid): Add handler for Back Key in Android

* Fix(Lint): fix lint error

* Fix(Lint): fix lint error
  • Loading branch information
Swordsman-Inaction authored and aksonov committed Aug 14, 2016
1 parent 813e075 commit 0ef1f7a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/API_CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
| children | | required (if no scenes property passed)| Scene root element |
| scenes | `object` | optional | scenes for Router created with Actions.create. This will allow to create all actions BEFORE React processing. If you don't need it you may pass Scene root element as children |
| getSceneStyle | `function` | optional | Optionally override the styles for NavigationCard's Animated.View rendering the scene. |
| backAndroidHandler | `function` | optional | Optionally override the handler for `BackAndroid`, return `true` to stay in the app or return `false` to exit the app. Default handler will pop a scene and exit the app at last when the back key is pressed on Android. |
| onBackAndroid | `function` | optional | Get called after back key is pressed and a scene is poped, won't affect the default behavior. |
| onExitApp | `function` | optional | Optionally override the default action after back key is pressed on root scene. Return `true` to stay, or return `false` to exit the app. |

## Scene:

Expand Down
6 changes: 6 additions & 0 deletions src/Reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import * as ActionConst from './ActionConst';
import { ActionMap } from './Actions';
import { assert } from './Util';
import { getInitialState } from './State';
import { Platform } from 'react-native';

// WARN: it is not working correct. rewrite it.
function checkPropertiesEqual(action, lastAction) {
Expand Down Expand Up @@ -90,6 +91,11 @@ function inject(state, action, props, scenes) {
case ActionConst.BACK:
case ActionConst.BACK_ACTION: {
assert(!state.tabs, 'pop() operation cannot be run on tab bar (tabs=true)');

if (Platform.OS === 'android') {
assert(state.index > 0, 'You are already in the root scene.');

This comment has been minimized.

Copy link
@Arnoldnuo

Arnoldnuo Aug 25, 2016

What's this?

}

if (state.index === 0) {
return state;
}
Expand Down
37 changes: 37 additions & 0 deletions src/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import React, {
Component,
PropTypes,
} from 'react';
import { BackAndroid } from 'react-native';
import NavigationExperimental from 'react-native-experimental-navigation';

import Actions, { ActionMap } from './Actions';
Expand All @@ -25,6 +26,9 @@ const {

const propTypes = {
dispatch: PropTypes.func,
backAndroidHandler: PropTypes.func,
onBackAndroid: PropTypes.func,
onExitApp: PropTypes.func,
};

class Router extends Component {
Expand All @@ -34,16 +38,49 @@ class Router extends Component {
this.state = {};
this.renderNavigation = this.renderNavigation.bind(this);
this.handleProps = this.handleProps.bind(this);
this.handleBackAndroid = this.handleBackAndroid.bind(this);
}

componentDidMount() {
this.handleProps(this.props);

BackAndroid.addEventListener('hardwareBackPress', this.handleBackAndroid);
}

componentWillReceiveProps(props) {
this.handleProps(props);
}

componentWillUnmount() {
BackAndroid.removeEventListener('hardwareBackPress', this.handleBackAndroid);
}

handleBackAndroid() {
const {
backAndroidHandler,
onBackAndroid,
onExitApp,
} = this.props;
// optional for customizing handler
if (backAndroidHandler) {
return backAndroidHandler();
}

try {
Actions.pop();
if (onBackAndroid) {
onBackAndroid();
}
return true;
} catch (err) {
if (onExitApp) {
return onExitApp();
}

return false;
}
}

handleProps(props) {
let scenesMap;

Expand Down

0 comments on commit 0ef1f7a

Please sign in to comment.