App icon indicating copy to clipboard operation
App copied to clipboard

Inconsistent Android back button behavior while navigated to a modal view

Open trjExpensify opened this issue 5 years ago • 56 comments

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:

  1. Open the settings modal
  2. Press the back button

Platform:

Android

Notes/Photos/Videos:

  • This issue is caused by a bug in react-navigation and 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

trjExpensify avatar Mar 25 '21 00:03 trjExpensify

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.

sohaibkhaa avatar Mar 25 '21 10:03 sohaibkhaa

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 avatar Mar 25 '21 19:03 ahmedahtazaz

@ahmedahtazaz Doesn't react-navigation gives that by default?

parasharrajat avatar Mar 25 '21 19:03 parasharrajat

@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.

ahmedahtazaz avatar Mar 25 '21 19:03 ahmedahtazaz

So in my point of view custom back button handling is needed in these scenarios.

ahmedahtazaz avatar Mar 25 '21 19:03 ahmedahtazaz

Hi,

This is Kaushik here and here is explanation for this,

  1. 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

kaushiktd avatar Mar 26 '21 16:03 kaushiktd

@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 avatar Mar 29 '21 16:03 marcaaron

@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. Screenshot 2021-03-29 at 11 34 14 PM

ahmedahtazaz avatar Mar 29 '21 18:03 ahmedahtazaz

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?

marcaaron avatar Mar 29 '21 19:03 marcaaron

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 avatar Mar 29 '21 19:03 marcaaron

@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.

ahmedahtazaz avatar Mar 29 '21 19:03 ahmedahtazaz

@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.

ahmedahtazaz avatar Mar 29 '21 19:03 ahmedahtazaz

Please let me know if I should start working on this or if you are delegating it to someone else.

ahmedahtazaz avatar Mar 29 '21 19:03 ahmedahtazaz

@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.

ahmedahtazaz avatar Mar 29 '21 20:03 ahmedahtazaz

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.

marcaaron avatar Mar 29 '21 21:03 marcaaron

If that doesn't work, we can return to discuss this workaround some more.

marcaaron avatar Mar 29 '21 21:03 marcaaron

Okay.

ahmedahtazaz avatar Mar 29 '21 21:03 ahmedahtazaz

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...

  1. Create a ModalScreenWrapper component for all screens that are presenting modally. It should wrap the screen ScreenWrapper component and capture the back handler handles like you've mentioned not allowing them to fall to the DrawerView (see my comments on the react-nav issue).
  2. We will just call navigation.goBack() when the back button is pressed.
  3. Pressing the back button while we are in a nested route like settings/password should bring us back to the previous view.
  4. 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/password then one back press should bring us to settings and another should reveal the open drawer.

As for hooks, if we can avoid them that would be best.

marcaaron avatar Apr 02 '21 01:04 marcaaron

@ahmedahtazaz @trjExpensify I think we're good to go here.

marcaaron avatar Apr 02 '21 01:04 marcaaron

@marcaaron kindly accept my upwork proposal so that we can start on this.

ahmedahtazaz avatar Apr 02 '21 13:04 ahmedahtazaz