React-native-navigation: How to disable android hardware back button with stack?

Created on 26 Dec 2018  路  7Comments  路  Source: wix/react-native-navigation

Issue Description

Hello, I'm using a stack in my app and whenever I press the back hardware button on android the app pop to the previous screen.

My issue is that i cannot override this behaviour, I have a BackHandler and a listener which is called whenever i press the hardware back button, but even if i have my own implementation the pop is still happening!

Is there a way to disable the hardware back button ?


Environment

  • React Native Navigation version: 2.2.5
  • React Native version: 57.2
  • Android

Most helpful comment

Try the following:

  componentDidMount() {
    BackHandler.addEventListener('hardwareBackPress', this.onBackPress);
  }

  componentWillUnmount() {
    BackHandler.removeEventListener('hardwareBackPress', this.onBackPress);
  }

  onBackPress = () => {
    return true; <---- make sure you return true.
  }

All 7 comments

Try the following:

  componentDidMount() {
    BackHandler.addEventListener('hardwareBackPress', this.onBackPress);
  }

  componentWillUnmount() {
    BackHandler.removeEventListener('hardwareBackPress', this.onBackPress);
  }

  onBackPress = () => {
    return true; <---- make sure you return true.
  }

@ItsNoHax, I forgot to mention it, but thats what I do and it still doesn't work

@ItsNoHax, i just solve this, the problem was not the return statement from onBackPress. The problem was that my listeners was never removed. Indeed i had multiple listeners for each of my screens and i was removing them in componentWillUnmount lifecycle method. But since I was using a stack the previous screens were never unmount so I had to remove my listener inside componentDidDisappear.

@ItsNoHax , this is awesome.

componentDidMount() {
BackHandler.addEventListener('hardwareBackPress', this.onBackPress);
}

componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this.onBackPress);
}

onBackPress = () => {
return true; <---- make sure you return true.
}

I want to disable device back button popping, in only one screen
Any idea?

Was this page helpful?
0 / 5 - 0 ratings