Reputation: 367
When I press the Backbutton it switches back to the previous screen but closes the app as well. I have already tried to add event.preventDefault() but it doesn't fix my issue.
componentWillMount() {
BackAndroid.addEventListener('hardwareBackPress', () => {
this.goBack();
})
}
goBack() {
this.props.actions.goBack();
}
//Reducer
export default function navReducer(state = initialState, action = {}) {
switch(action.type) {
case types.OPEN:
return {
scenes: [
...state.scenes,
{key: action.key, name: action.name}
]
};
case types.BACK:
if(state.scenes.length > 1) {
return {
scenes: state.scenes.slice(0, state.scenes.length - 1)
}
} else {
console.log('Error')
};
default:
return state;
}
}
Upvotes: 1
Views: 101
Reputation: 1264
Update your goBack method as:
goBack() {
this.props.actions.goBack();
return true;
}
You have to return true from the goback method, otherwise it will close the app.
Upvotes: 2