dotnet / dotnet/maui

Shell.OnNavigating and Shell.OnNavigated is not consitent

Open
#7,351 5 comments 0 reactions 0 assignees View on GitHub
area-controls-shell delighter platform/android platform/ios s/triaged s/verified t/bug
Dominant language
C#
Stars
23.3k
Forks
2k
Avg merge
1d 15h
Merged PRs (30d)
290

Description

### Description

As a developer I was expecting to have a consistent call to the override OnNavigated and OnNavigating methods of my AppShell class.

On this [sample project](https://github.com/ederbond/Shell_OnNavigated_Bug) I have 4 Pages:

- PageA,
- PageB,
- PageC (Modal)
- PageD

So I'm performing the following steps:

1. Let shell startup with `PageA`.
2. Navigate to `PageB `reseting the navigation stack
3. Navigate modal to `PageC`
4. Press back button to go back to `PageB`
5. Navigate to `PageD`

These are the calls that I currently get from my AppShell running on Android:

1. App Startup
[0:] ---------------------------- OnNavigated ----------------------------
[0:] args.Source: ShellItemChanged
[0:] Shell.Current.CurrentPage : PageA
[0:] ------------------------------------------------------------------------

2. Navigate to `PageB `reseting the navigation stack
[0:] ---------------------------- OnNavigating ----------------------------
[0:] args.Source: ShellItemChanged
[0:] Shell.Current.CurrentPage : PageA
[0:] ------------------------------------------------------------------------

[0:] ---------------------------- OnNavigated ----------------------------
[0:] args.Source: ShellItemChanged
[0:] Shell.Current.CurrentPage : PageB
[0:] ------------------------------------------------------------------------

3. Navigate modal to `PageC`
[0:] ---------------------------- OnNavigating ----------------------------
[0:] args.Source: Push
[0:] Shell.Current.CurrentPage : PageB
[0:] ------------------------------------------------------------------------

[0:] ---------------------------- OnNavigated ----------------------------
[0:] args.Source: Push
[0:] Shell.Current.CurrentPage : PageC
[0:] ------------------------------------------------------------------------

4. Press back button to go back to `PageB`
[0:] ---------------------------- OnNavigating ----------------------------
[0:] args.Source: PopToRoot
[0:] Shell.Current.CurrentPage : PageC
[0:] ------------------------------------------------------------------------

[0:] ---------------------------- OnNavigated ----------------------------
[0:] args.Source: PopToRoot
[0:] Shell.Current.CurrentPage : PageB
[0:] ------------------------------------------------------------------------

5. Navigate to `PageD`
[0:] ---------------------------- OnNavigating ----------------------------
[0:] args.Source: ShellItemChanged
[0:] Shell.Current.CurrentPage : PageB
[0:] ------------------------------------------------------------------------

[0:] ---------------------------- OnNavigated ----------------------------
[0:] args.Source: ShellItemChanged
[0:] Shell.Current.CurrentPage : PageD
[0:] ------------------------------------------------------------------------

### EXPECTED BEHAVIOR:

On 1. App Startup I was expecting to get the OnNavigated and OnNavigating methods called with these parameters:
[0:] ---------------------------- OnNavigating ----------------------------
[0:] args.Source: Push
[0:] Shell.Current.CurrentPage : PageA
[0:] ------------------------------------------------------------------------
[0:] ---------------------------- OnNavigated ----------------------------
[0:] args.Source: Push
[0:] Shell.Current.CurrentPage : PageA
[0:] ------------------------------------------------------------------------

2. Navigate to `PageB `reseting the navigation stack
[0:] ---------------------------- OnNavigating ----------------------------
[0:] args.Source: Push
[0:] Shell.Current.CurrentPage : PageB
[0:] ------------------------------------------------------------------------

[0:] ---------------------------- OnNavigated ----------------------------
[0:] args.Source: Pop
[0:] Shell.Current.CurrentPage : PageA
[0:] ------------------------------------------------------------------------

3. Navigate modal to `PageC`
[0:] ---------------------------- OnNavigating ----------------------------
[0:] args.Source: Push
[0:] Shell.Current.CurrentPage : PageC
[0:] ------------------------------------------------------------------------

[0:] ---------------------------- OnNavigated ----------------------------
[0:] args.Source: Push
[0:] Shell.Current.CurrentPage : PageC
[0:] ------------------------------------------------------------------------

4. Press back button to go back to `PageB`
[0:] ---------------------------- OnNavigating ----------------------------
[0:] args.Source: Pop or PopToRoot
[0:] Shell.Current.CurrentPage : PageC
[0:] ------------------------------------------------------------------------

[0:] ---------------------------- OnNavigated ----------------------------
[0:] args.Source: Pop or PopToRoot
[0:] Shell.Current.CurrentPage : PageC
[0:] ------------------------------------------------------------------------

5. Navigate to `PageD`
[0:] ---------------------------- OnNavigating ----------------------------
[0:] args.Source: ShellItemChanged
[0:] Shell.Current.CurrentPage : PageB
[0:] ------------------------------------------------------------------------

[0:] ---------------------------- OnNavigated ----------------------------
[0:] args.Source: Push
[0:] Shell.Current.CurrentPage : PageD
[0:] ------------------------------------------------------------------------

The way that Shell is currently behaving can't allow me to use it's OnNavigating and/or OnNavigated overrides to safely dispose my pages and/or view models, cause I can't precisely determine when a given page was poped/removed from the navigation stack.

### Steps to Reproduce

Clone this repo:
https://github.com/ederbond/Shell_OnNavigated_Bug
and run the app performing the navigation in the order that I described and see the logs on Visual Studio Output Window

### Version with bug

Release Candidate 3 (current)

### Last version that worked well

Unknown/Other

### Affected platforms

iOS, Android

### Affected platform versions

Android 11 and probably iOS

### Did you find any workaround?

No

### Relevant log output

_No response_

Contributor guide

Open the contributing guide

Research direction

Clone and run the linked Shell_OnNavigated_Bug sample, then reproduce the five navigation steps and compare the logs from AppShell.OnNavigating and AppShell.OnNavigated with the expected sequences. Trace the Shell navigation entry points involved in startup, stack reset, modal push, back navigation, and item changes; done means callbacks report consistent sources and current pages on Android and iOS.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
mobile-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.