From e9284c8fd9d23f28a91822c58a3cd676bd1a60d4 Mon Sep 17 00:00:00 2001 From: Swordsman-Inaction Date: Wed, 15 Jun 2016 20:14:31 +0800 Subject: [PATCH 1/3] Feature(Handle Back Key for Android) --- src/Reducer.js | 6 ++++++ src/Router.js | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/Reducer.js b/src/Reducer.js index 510d72897..001ba7cd4 100644 --- a/src/Reducer.js +++ b/src/Reducer.js @@ -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) { @@ -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; } diff --git a/src/Router.js b/src/Router.js index 43ff28384..04ad9965e 100644 --- a/src/Router.js +++ b/src/Router.js @@ -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'; @@ -98,6 +99,16 @@ class Router extends Component { render() { if (!this.state.reducer) return null; + BackAndroid.addEventListener('hardwareBackPress', () => { + try { + Actions.pop(); + return true; + } + catch (err) { + return false; + } + }) + return ( Date: Wed, 15 Jun 2016 20:15:53 +0800 Subject: [PATCH 2/3] Fix(format) --- src/Reducer.js | 4 ++-- src/Router.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Reducer.js b/src/Reducer.js index 001ba7cd4..0c63e4f29 100644 --- a/src/Reducer.js +++ b/src/Reducer.js @@ -75,9 +75,9 @@ function inject(state, action, props, scenes) { 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.') + assert(state.index > 0, 'You are already in the root scene.'); } - + if (state.index === 0) { return state; } diff --git a/src/Router.js b/src/Router.js index 04ad9965e..11736b858 100644 --- a/src/Router.js +++ b/src/Router.js @@ -10,7 +10,7 @@ import React, { Component, PropTypes, } from 'react'; -import { BackAndroid } from 'react-native' +import { BackAndroid } from 'react-native'; import NavigationExperimental from 'react-native-experimental-navigation'; import Actions from './Actions'; @@ -107,7 +107,7 @@ class Router extends Component { catch (err) { return false; } - }) + }); return ( Date: Wed, 15 Jun 2016 20:24:25 +0800 Subject: [PATCH 3/3] Fix(format) --- src/Router.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Router.js b/src/Router.js index 11736b858..751d352c0 100644 --- a/src/Router.js +++ b/src/Router.js @@ -103,8 +103,7 @@ class Router extends Component { try { Actions.pop(); return true; - } - catch (err) { + } catch (err) { return false; } });