xamarin/Xamarin.Forms

[Bug/Question] Shell routing/navigation reuses the same page instance on every call to Shell.GoToAsync()

Open

#7,555 opened on Sep 17, 2019

 (6 comments) (3 reactions) (0 assignees)C# (1,926 forks)batch import
a/shell :shell:e/3 :clock3:help wantedt/bug :bug:up-for-grabs

Repository metrics

Stars
 (5,644 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

Description

The Shell's navigation logic appears to only ever create a single instance of the Page registered to a given route, and then reuses it for all navigation requests to that route.

I'm not sure if this is a bug, or if it's intentional and I'm just using it incorrectly. I imagine it's the latter, so further direction would be appreciated.

Steps to Reproduce

Given the attached sample project: ShellNavRepro.zip

  1. Run the app and observe that MainPage is initialized and displayed.
  2. Tap the "Another" link in the flyout menu, and observe that the AnotherPage is initialized and displayed.
  3. Tap the "Main" link in the flyout menu, and observe that the same instance of MainPagecreated in step 1 is shown.
  4. Tap the "Another" link in the flyout menu, and observe that the same instance of AnotherPage created in step 2 is shown.

Expected Behavior

I expected new instances of pages to be created any time their menu items are tapped (i.e. their routes are navigated to). Likewise, whenever a screen is navigated away from, I expected the page to be discarded/disposed.

There are at least a couple of issues that I'm having with this behavior:

  1. I can't tell the difference between when a screen is displayed for the first time, and when it is being displayed again after foregrounding the app (the Page.OnAppearing() method is called either way).
  2. Pages with route parameters (i.e. via the QueryPropertyAttribute) can end up in states where you don't know if the parameters were set from the current route navigation attempt or a previous attempt, since route parameters are only set/reset/cleared if they are specified. So invoking a route with no parameters after having previously invoked the route with parameters leaves the parameters set (i.e. since the same Page instance is used),

Actual Behavior

Page instances are reused between separate navigation attempts.

Basic Information

  • Version with issue: 4.2.0.778463

Contributor guide