React-native-navigation: How to override back button action

Created on 20 Nov 2017  路  20Comments  路  Source: wix/react-native-navigation

Platform : android
react-native-navigation: 1.1.x

By default back button action is to pop to previous screen.

But I would like to override this back button action and would like to call below method to perform custom action,
onBackButtonPress(){
doSomeOperation();
this.props.navigator.pop();
}

Is it possible to override default back button action?

Most helpful comment

Yes you can. I do it this way:

In your screen:

import { BackHandler } from 'react-native';

...
...

constructor(props) {
    super(props);
    this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
}

onNavigatorEvent(event) {
    switch (event.id) {
      case 'willAppear':
        this.backHandler = BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
        break;
      case 'willDisappear':
        this.backPressed = 0;
        this.backHandler.remove();
        break;
      default:
        break;
    }
  }

  handleBackPress = () => {
    if (this.backPressed && this.backPressed > 0) {
      this.props.navigator.popToRoot({ animated: false });
      return false;
    }

    this.backPressed = 1;
    this.props.navigator.showSnackbar({
      text: 'Press one more time to exit',
      duration: 'long',
    });
    return true;
  }

All 20 comments

Yes you can. I do it this way:

In your screen:

import { BackHandler } from 'react-native';

...
...

constructor(props) {
    super(props);
    this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
}

onNavigatorEvent(event) {
    switch (event.id) {
      case 'willAppear':
        this.backHandler = BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
        break;
      case 'willDisappear':
        this.backPressed = 0;
        this.backHandler.remove();
        break;
      default:
        break;
    }
  }

  handleBackPress = () => {
    if (this.backPressed && this.backPressed > 0) {
      this.props.navigator.popToRoot({ animated: false });
      return false;
    }

    this.backPressed = 1;
    this.props.navigator.showSnackbar({
      text: 'Press one more time to exit',
      duration: 'long',
    });
    return true;
  }

Thankyou @Fuhrmann, its working.

@Fuhrmann any way to make this with the app back button too? it works with the phone back button but the app back button keeps going to the previous screen

@JFE84 I am not on pc right now, but maybe you could try this: https://github.com/wix/react-native-navigation/issues/1135#issuecomment-319724858.

Set an ID to your back button and catch the event in the navigator using setOnNavigatorEvent. I didn't test this approach, so I don't know if it will work.

@Fuhrmann yes that works, thanks. I was having issues with this solution because I was using id='back' and it wasn't working. It was doing the default thing to go previous screen and onNavigatorEvent wasn't even getting the event
When I tried id=anyWordThatIWant it worked. Guess it is because id='back' is reserved and it automatically triggers the default action to go to the previous screen

@JFE84 Nice, good to know! :)

Now that overrideBackPress: true is bugged on Android, @Fuhrmann solution has saved me, thanks! 馃憤

thank you @Fuhrmann you save my day.

@Fuhrmann I have to put the above code in all screens?

@ashokkumar88 I do not work with RN anymore, but as I remember yes. I guess it depends on your screens setup.

@Fuhrmann Thanks for quick reply. So did you find any good alternative for React Native. If yes, then please suggest.

@ashokkumar88 Unfortunately not. I had to stick with Android development.

@Fuhrmann can you check this issue.I used your solution.But It did not work for me :( .
Here I attched stckowerflow link stackoverflow

@HRCJ7 after login you have to call this.props.navigator.resetTo({ screen:SCREEN});

how to do this on the v2 version ?

@Lokendra-rawat did you find a way for intercepting back button? I want to override back hardware press

@Fuhrmann 's answer is for Android only, although the question is for iOS as well.

Yes you can. I do it this way:

In your screen:

```
import { BackHandler } from 'react-native';

...
...

constructor(props) {
super(props);
this.props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
}

onNavigatorEvent(event) {
switch (event.id) {
case 'willAppear':
this.backHandler = BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
break;
case 'willDisappear':
this.backPressed = 0;
this.backHandler.remove();
break;
default:
break;
}
}

handleBackPress = () => {
if (this.backPressed && this.backPressed > 0) {
this.props.navigator.popToRoot({ animated: false });
return false;
}

this.backPressed = 1;
this.props.navigator.showSnackbar({
  text: 'Press one more time to exit',
  duration: 'long',
});
return true;

}
```Could you explain with some better example .I'm unable to understand this.
I used transitionConfig to add animation while switching between the screens.To clear the previous screen stack I used StackActions but this removed my animation between the screens.

how to use this behavior on V2 aswell?

@cinder92 doesn't have this on v2. You have to override backButton icon, by passing an id and an icon.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

viper4595 picture viper4595  路  3Comments

kiroukou picture kiroukou  路  3Comments

yedidyak picture yedidyak  路  3Comments

EliSadaka picture EliSadaka  路  3Comments

switchtrue picture switchtrue  路  3Comments