Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
react-native-router-flux, handle android back button event.
import { BackAndroid } from 'react-native';
import {Reducer} from 'react-native-router-flux';
backButtonPressedOnceToExit = false;
let currentSceneName = null;
const reducerCreate = params=>{
const defaultReducer = Reducer(params);
return (state, action)=>{
console.info("reducerCreate.{state,action}", state, action);
var nextState = defaultReducer(state, action);
console.info("reducerCreate.nextState:", nextState);
//do something...
currentSceneName = getCurrentName(nextState);
return nextState
}
};
function getCurrentName(obj) {
const { index, children, name } = obj;
if (index !== undefined) {
if (children) {
return getCurrentName(children[index]);
}
}
return name;
}
export default class Main extends React.Component {
...
componentWillMount(){
BackAndroid.addEventListener('hardwareBackPress', this.onBackAndroid.bind(this));
}
componentWillUnmount(){
BackAndroid.removeEventListener('hardwareBackPress', this.onBackAndroid.bind(this));
}
onBackAndroid() {
if (backButtonPressedOnceToExit) {
BackAndroid.exitApp();
} else {
if ([
'tab1',
'tab2',
'tab3',
'tab4',
'tab5'
].indexOf(currentSceneName) === -1) {
Actions.pop();
return true;
} else {
backButtonPressedOnceToExit = true;
console.log('Press again to exit APP');
setTimeout(function() {
backButtonPressedOnceToExit = false;
}, 2000);
return true;
}
}
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.