Ludwig Bossle
Ludwig Bossle

Reputation: 367

BackAndroid - App still closes

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

Answers (1)

Jagadish Upadhyay
Jagadish Upadhyay

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

Related Questions