[Bug/Question] Shell routing/navigation reuses the same page instance on every call to Shell.GoToAsync()
#7,555 opened on Sep 17, 2019
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
- Run the app and observe that
MainPageis initialized and displayed. - Tap the "Another" link in the flyout menu, and observe that the
AnotherPageis initialized and displayed. - Tap the "Main" link in the flyout menu, and observe that the same instance of
MainPagecreated in step 1 is shown. - Tap the "Another" link in the flyout menu, and observe that the same instance of
AnotherPagecreated 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:
- 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). - 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 samePageinstance is used),
Actual Behavior
Page instances are reused between separate navigation attempts.
Basic Information
- Version with issue: 4.2.0.778463