Inconsistent Android back button behavior while navigated to a modal view
If you haven’t already, check out our contributing guidelines for onboarding!
Expected Result:
The Android hardware back button should work when pressed.
Actual Result:
When you open the settings page the first back button press is ignored (as if an extra invisible page was added to the page stack), but the second and third back button press work as expected.
Action Performed:
- Open the settings modal
- Press the back button
Platform:
Android
Notes/Photos/Videos:
- This issue is caused by a bug in
react-navigationand must be fixed in that repo first. - See: https://github.com/react-navigation/react-navigation/issues/9364
- Workarounds will not be accepted.
Expensify/Expensify Issue URL: https://github.com/Expensify/Expensify/issues/157248 Upwork link: https://www.upwork.com/jobs/~016d2a711284ef2fc7
I will fix the issue by manipulating the app behaviour and debugging to check whether is there any other page opened while opening the settings page. After finding the bug try to resolve it by updating the part of code that is used to open up settings page. And debug the back press behaviour of settings page.
While reviewing through the job I have noticed that there is no custom hardware back button handling in your app. Well in my experience it is a must for Android. This is a normal issue while building cross-platform apps with React Native because the developer is mostly focused on one platform and expect it to work on both of them.
@ahmedahtazaz Doesn't react-navigation gives that by default?
@parasharrajat Yes react-navigation gives it by default but there are some scenarios in which it does not work properly. That's the very reason there are lots of back button issues reported over the time specially for Android. You can check stack-overflow for relevant issues.
So in my point of view custom back button handling is needed in these scenarios.
Hi,
This is Kaushik here and here is explanation for this,
- Maybe a configuration issue in the stack, will be resolved by stack management or manage back handler on a specific page on the single back
@ahmedahtazaz I'm not too sure what the exact proposal is here yet. Can you briefly outline the changes you intend to make and point to some specific area in the code?
@marcaaron I have attached a screenshot that points to the point in code where I will add a custom back button handler. This handler will receive all the back button press events. I will check the routes here and if the current route is settings/preferences i will executes this function: Navigation.navigate(ROUTES.SETTINGS)Navigation.navigate(ROUTES.SETTINGS)
In this way the back button handling will be delegated to the PreferencesPage instead of HeaderWithCloseButton.
I hope my solution is clear now.

It's still unclear to me sorry. The docs state:
By default, when user presses the Android hardware back button, react-navigation will pop a screen or exit the app if there are no screens to pop. This is a sensible default behavior, but there are situations when you might want to implement custom handling.
If the "sensible default behavior" is that a back button press should return us to the last place we were (which in this case should dismiss the settings screen and display the side bar again).
But the issue states:
When you open the settings page the first back button press is ignored
So, I don't see how the custom back button handler helps us here. Why is the first back button press ignored?
Looked into this a bit and surely this is the bug here...
https://github.com/react-navigation/react-navigation/issues/9364
Ideally, this would get fixed in the react-navigation library. But I think we'll need to implement this same solution in all screens. So if we implement the work around it should happen in the ScreenWrapper component and we should link to that issue so it can be fixed by upgrading eventually.
@marcaaron here is the catch. When I will add a custom back button handler the first hardware back press will give me the actual route or the actual current route in the navigation stack. Then we will have a very good understanding if there is a screen on top of the preferences screen or not. Custom back handling will be used as a debugging strategy here as well.
@marcaaron as a matter of fact I did not know about this bug react-navigation/react-navigation#9364.
Thanks for this information. Yes implementation of custom back handler in ScreenWrapper will be generic and will fix this issue in all screens where this wrapper is being used.
Please let me know if I should start working on this or if you are delegating it to someone else.
@marcaaron One another thing we will add custom back handler in ScreenWrapper but will only use this handling for those where the problem occurs. I think this problem is not consistent across all screens so we will only do this for setting/preferences route. If there are more problematic routes later in app development then we will add those routes with time in this handling.
There is also a style guideline ambiguity here as well ScreenWrapper is functional component and we will have to use hooks here now or we should convert it to class based component if we are avoiding hooks.
Kindly clear these questions as well before we proceed.
In this case, the most correct solution seems to be to submit a fix PR to react-navigation repo and solve this bug by updating to the latest version.
If that doesn't work, we can return to discuss this workaround some more.
Okay.
Alright, so I've looked into this issue some more and I don't think it's something that will be fixed in react-navigation any time soon (more context here). The default behavior of the drawer navigator is to close the drawer when the back button is pressed and it doesn't take into account screens that are rendered modally above it in a stack navigator.
Based on all that, I think the custom back handler is our best bet to solve this problem.
Building on your initial proposal @ahmedahtazaz we should...
- Create a
ModalScreenWrappercomponent for all screens that are presenting modally. It should wrap the screenScreenWrappercomponent and capture the back handler handles like you've mentioned not allowing them to fall to theDrawerView(see my comments on the react-nav issue). - We will just call
navigation.goBack()when the back button is pressed. - Pressing the back button while we are in a nested route like
settings/passwordshould bring us back to the previous view. - Deep links should be able to navigate back to the open drawer (or subroute) with a back button press. So e.g. if we open the app directly to
settings/passwordthen one back press should bring us tosettingsand another should reveal the open drawer.
As for hooks, if we can avoid them that would be best.
@ahmedahtazaz @trjExpensify I think we're good to go here.
@marcaaron kindly accept my upwork proposal so that we can start on this.