question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

closeOnHardwareBackPress not working (Android back button keeps closing alert)

See original GitHub issue

I’m on a Samsung Galaxy S6 with Android 7.

        <AwesomeAlert
            show={!assistanceConfirmed}
            showProgress
            title="Esperando confirmación de asistencia"
            closeOnHardwareBackPress={false}
            closeOnTouchOutside={false}
            overlayStyle={styles.overlay}
          />

Am I missing something?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
smerkousdavidcommented, Aug 26, 2018

The core issue has been fixed with new update, but the back button doesn’t allow me to use the previous/custom BackHandlers that I initially defined. After looking at your code and the documentation for BackHandlers I realized that your BackHandler returns true even if it’s not handling the springHide, which causes other listeners to break.

  _handleHwBackEvent = () => {
    const { closeOnHardwareBackPress } = this.props;
    if (this.state.showSelf && closeOnHardwareBackPress) {
      this._springHide();
      return true;
    } else if (!closeOnHardwareBackPress && this.state.showSelf) {
      return true;
    }

    return false;
  };

According to the docs https://facebook.github.io/react-native/docs/backhandler. The listeners are registered and called in reverse order and it stops when the first function returns true. This means you still have a listener active and returning true before it hits listeners that are defined before AwesomeAlert’s.

You could just change

    } else if (!closeOnHardwareBackPress && this.state.showSelf) {
      return true;
    }

to

    } else if (!closeOnHardwareBackPress && this.state.showSelf) {
      return false;
    }

I think you designed it like this so that it doesn’t exit the app when the Alert is the only registered hardware back event listener. I see another solution being wrapping the event listener in a closeHardwareBackPress condition if(this.props.closeOnHardwareBackPress) HwBackHandler.addEventListener(HW_BACK_EVENT, this._handleHwBackEvent);.

I hope this helps 👍

Thanks for the awesome library @rishabhbhatia

1reaction
rishabhbhatiacommented, Aug 24, 2018

Ok @jjercx , I’ll fix it tomorrow

Read more comments on GitHub >

github_iconTop Results From Across the Web

Preventing hardware back button android for React Native
So I added code, but this does not work. Are there any solutions for this? The alert pop up is seen but "return...
Read more >
Handling Android Back Button Press in React Native
How to handle Android back button press in React Native using React Navigation. We will use useFocusEffect for handling Android back button.
Read more >
Android Back Button handling in React Native apps — 1x08
Method removeAndroidBackButtonHandler removes the previously added event listener in order to secure that we will not keep adding event listeners when we ...
Read more >
Android Back Button causes crashing/closing - sometimes
Pressing the Back key will close/crash my app about 50% of the time. I ... (I was able to reduce the problem by...
Read more >
Handling Android Back Button Press in React Native
In this tutorial we are going to discuss how to make the App Quit When Hardware Back Button is pressed in React Native...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found