Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android back button callback? #13

Closed
yairk opened this issue Apr 3, 2017 · 6 comments
Closed

Android back button callback? #13

yairk opened this issue Apr 3, 2017 · 6 comments

Comments

@yairk
Copy link
Contributor

yairk commented Apr 3, 2017

By default, the Android Back button will close the modal. Looking at the code I found the (undocumented?) hideOnBack property which disabled this.

The behavior I'm after is a bit different: I'm presenting the user with an alert modal, on top of an existing scene, that will explain what needs to be done. What I'd like to happen is that if Back is pressed, then the app will navigate to the earlier scene (i.e. not just close the modal and stay at the same scene, but go back to the previous scene).

I was hoping that a close callback would allow me to do this (by simulating a Back press), but I don't think I can tell whether the modal was closed as a result of the Back button or not. Is there a way to achieve this?

@mmazzarolo
Copy link
Member

Maybe I'm missing something but isn't onModalHide enough to achieve your desired effect?
You just navigate to the previous screen when onModalHide is triggered. 🤔

Thank you for noticing the hideOnBack missing documentation, I'm adding it right now :)

@yairk
Copy link
Contributor Author

yairk commented Apr 3, 2017

Thanks for the quick reply!

The thing is that I don't think I can tell that onModalHide was called as a result of the Back button being pressed. In my particular case, there are other triggers that can cause the modal to go away, and I want to treat the Back button differently.

@mmazzarolo
Copy link
Member

mmazzarolo commented Apr 3, 2017

I think I got it now :)
Did you already try handling the back button press by catching it in the modal's parent component (through react-native's backandroid)?
I'm not a master of the Android back button functionality but it seems that toying a bit with it should solve your issue.

If it doesn't work I guess another solution would be making this component close when changing it's isVisible prop, but it would require a bit more code on my side... so let me know if the first solution works first! :)

@yairk
Copy link
Contributor Author

yairk commented Apr 3, 2017

I have a listener for back button at the very "top level" of my app, and when I set hideOnBack to false my listener isn't getting called. I guess that react-native's Modal swallows the notification somehow. I actually didn't try installing a handler in the modal's parent component. Will give it a try.

Not sure I follow what you meant by closing when changing isVisible, but would it be possible for react-native-modal to expose a callback from closeOnBack (or maybe provide an argument to the onModalHide callback that indicates wasClosedByBack)? I don't think this should require too much code. I can try it tomorrow and send it as a pull request if it makes sense to you.

@mmazzarolo
Copy link
Member

Honestly, I wanted to preserve closeOnBack/onRequestClose to the current status for another nifty future I planned (hiding modal on backdrop press) but in the end add a callback shouldn't interfere too much at all so... sure, you can do it, a PR is very much welcome :)

Thanks!

@mmazzarolo
Copy link
Member

Released on v2.2.0, closing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants