Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Hand Back Key for Android #820

Merged
merged 3 commits into from
Jun 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {

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 @@ -72,6 +73,11 @@ function inject(state, action, props, scenes) {
case POP_ACTION2:
case POP_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.');
}

if (state.index === 0) {
return state;
}
Expand Down
10 changes: 10 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 from './Actions';
Expand Down Expand Up @@ -98,6 +99,15 @@ class Router extends Component {
render() {
if (!this.state.reducer) return null;

BackAndroid.addEventListener('hardwareBackPress', () => {
try {
Actions.pop();
return true;
} catch (err) {
return false;
Copy link

@xseignard xseignard Jun 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not something like this instead this return false;:

return typeof this.handleAndroidExit === 'function' ? this.handleAndroidExit() : false;

Where the handleAndroidExit() is a function passed as a props to the Router. Like this people would be able to define custom exit behavior and/or rely to the default one.

This would do the trick I guess.

}
});

return (
<NavigationRootContainer
reducer={this.state.reducer}
Expand Down