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

[BUG] Android "hardware" back button closes the app when app contains a tabbed page with a navigation page as root page for one of the tabs, Take 2 #3123

Open
skha83 opened this issue Apr 10, 2024 · 4 comments

Comments

@skha83
Copy link

skha83 commented Apr 10, 2024

Description

Sorry for creating another issue with more or less the same problem as was closed in #3095.
I have tried the navigation changes introduced in version 9.0.436-pre which should resolve issue #3095. I'm still having problems getting the navigation to work the way I expect it to work. And in one of the scenarios it work worse than before the change (scenario 1 below).
I have created a fork on the current Prism main and simplified my example so it only contain one tabbed page with a navigation page and two segments here https://github.com/skha83/Prism

The example code is modified like the following:

.CreateWindow(nav => nav.CreateBuilder()
    .AddTabbedSegment(page =>
            page.CreateTab(t =>
                t.AddNavigationPage()
                 .AddSegment("ViewA")
                 .AddSegment("ViewB")))
    .NavigateAsync())

Steps to Reproduce

I have two scenarios and both of them fail.

Scenario 1.

  1. Deploy and launch the PrismMauiDemo app from my repository on an Android phone ( tested on Samsung Galaxy S24 Ultra).
  2. The app is launched with ViewB page on top and ViewA and NavigationPage below.
  3. Press the hardware back button.

Expected result:
The app will navigate from ViewB -> ViewA but stay in foreground so ViewA is visible.

Actual result:
The app navigates to ViewA but immediately backgrounds the app. When the app is opened again ViewA is the current window. But the app is not terminated.

Scenario 2.

  1. Deploy and launch the PrismMauiDemo app from my repository on an Android phone (tested on Samsung Galaxy S24 Ultra).
  2. The app is launched with ViewB page on top and ViewA and NavigationPage below.
  3. Press the software navigate back button on the top left of the app, so it navigates back to ViewA.
  4. When ViewA is visible press the hardware back button.

Expected result:
The app will be backgrounded but not completely terminated.

Actual result:
The app gets completely terminated and needs to be relaunched and is started from scratch.

The behavior in scenario 1 is different from before the new changes introduced to fix issue #3095. Previously it would correctly navigate from ViewB -> ViewA and not background the app if the hardware back button is only pressed once.

Scenario 2 is working in the same way as before the recent changes.

Platform with bug

.NET MAUI

Affected platforms

iOS, Android

Did you find any workaround?

It seems like maonaoda have made some workaround #2990 (comment), but that requires introducing a custom NavigationPage class and then it is no longer possible to use the extension methods on the NavigationBuilder like AddNavigationPage. I would prefer this basic scenario worked "out of the box" so people wouldn't have to look around for a hack. I have not verified if his fix would work in my scenario.

Relevant log output

No response

@skha83 skha83 changed the title [BUG] <title>[BUG] Android "hardware" back button closes the app when app contains a tabbed page with a navigation page as root page for one of the tabs, Take 2 [BUG] Android "hardware" back button closes the app when app contains a tabbed page with a navigation page as root page for one of the tabs, Take 2 Apr 11, 2024
@PauchardThomas
Copy link

Same problem !

@dansiegel dansiegel added MAUI and removed to verify labels May 26, 2024
@dansiegel
Copy link
Member

fixed by #3148

@skha83
Copy link
Author

skha83 commented May 28, 2024

Hi @dansiegel while the recently introduced error of getting double back event has been fixed by #3148 the original problem of both the issue in scenario 2 of this issue and my original issue in #3095 still persist.
The problem was that the application would terminate when pressing the Android back button when standing on the root page of the navigation stack on a given tab page, instead of just putting the app into the background. This is at least my expected behavior,

The problem occur because a NavigationException.CannotPopApplicationMainPage occurs in

if (result.Exception is NavigationException { Message: NavigationException.CannotPopApplicationMainPage })
which I also already described in the original issue #3095 . This happens because it cannot pop the root page which makes sense. But instead of quitting the whole application it should just put it into background, which is the normal behavior for the apps I normally use. The underlying problem is probably in the call to navigationService.GoBackAsync() in this line
var result = await navigationService.GoBackAsync();
it should not throw an exception result but rather put the app into the background.

I know you are a busy man, but I hope this will get fixed at some point. For now I will stop creating new issues on the problem, as I don't want to pollute with new issues repeating myself.

@dansiegel dansiegel reopened this May 28, 2024
@mdartu
Copy link

mdartu commented May 30, 2024

We are hitting this problem, in what I believe to be a slightly different flavour that is not covered in #3148:

We have a NavigationPage that contains a TabbedPage, with a button to navigate to another TabbedPage. When navigating back using the system back button, the app is sent to the background. When using the soft back button provided by NavigationPage, the navigation is handled as expected.
The structure is somewhat like this: navigate back from PrismNavigationPage <- SomeTabbedPage <- SomePage to PrismNavigationPage <- SomeOtherTabbedPage <- SomeOtherPage. When using the system back button, the initial back navigation is handled (correctly) from PrismNavigationPage.OnBackButtonPressed(): a breakpoint in MvvmHelpers.HandleNavigationPageGoBack() is hit and back navigation is handled there. However, the callback passed in android.OnBackPressed (PrismAppBuilder.cs) is also called. When this happens, window.CurrentPage is SomePage, window.CurrentPage.Parent is SomeTabbedPage and window.CurrentPage.Parent.Parent is PrismNavigationPage, so the code does not hit the check around line 84 and false is returned, likely telling the OS to put the window in the background.

The behaviour that I expect is that the soft back button behaves the same as the hardware back button. The hardware back button should put the app in the background (not stop the process) when the navigation stack is empty.

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

4 participants