4

I've a login modal opened using showModal().

It has no navbar buttons, so ios users cannot close this modal.

Problem: Actually Android users can use hardware back button to close the login modal.

In my login modal I tried to add

BackHandler.addEventListener('hardwareBackPress', function() { return true; }

to disallow backbutton on Android, but it simply doesn't works.

I did this because I read what follows on official RN guide:

Android: Detect hardware back button presses, and programmatically invoke the default back button functionality to exit the app if there are no listeners or if none of the listeners return true.

Adding a console.log into this function I see the event fired on 'normal' screens but NOT when I've a modal showed !

What am i doing wrong?

realtebo
  • 23,922
  • 37
  • 112
  • 189
  • If your app shows an opened Modal, BackHandler won't publish any events! In this case you have to use `onRequestClose` - Property of the Modal (see https://facebook.github.io/react-native/docs/modal#onrequestclose) – suther Feb 12 '20 at 10:36

1 Answers1

4

Overriding hardware back button is possible using overrideBackPress property as described here

You can handle the back press in your component:

onNavigatorEvent(event) {
    if (event.id === 'backPress') {
        //Do your thing
    }
}
guy.gc
  • 3,359
  • 2
  • 24
  • 39