Bug 56287 - Page must not already have a parent exception if navigation occur on cached page and navigating to and pressing back button too fast.
Summary: Page must not already have a parent exception if navigation occur on cached p...
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.5.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2017-05-12 13:23 UTC by Bart Calixto
Modified: 2018-03-12 23:22 UTC (History)
7 users (show)

Tags: android ios navigation exception ac
Is this bug a regression?: ---
Last known good build:

repro project (296.78 KB, application/x-zip-compressed)
2017-05-17 19:28 UTC, Jimmy [MSFT]

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 56287 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description Bart Calixto 2017-05-12 13:23:55 UTC
When doing navigation back and forth using cached pages I got:
Unhandled Exception:
System.InvalidOperationException: Page must not already have a parent.

When trying to isolate the issue I manage to get a way to repro (but only using the UI).

To repro one needs navigate between pages as quick as possible while the page navigated too is cached in memory.
Clicking as fast as possible in a back-forth will eventually give you an exception. It normally takes me between 4-10 tries to get the exception.

If I navigate in a slow fashion it does NOT throw an exception.
If I create a new page everytime I do navigate (NoCachedPage) it does NOT throw an exception.

(I was able to reproduce this on :, and
Test were done on iOS real device running 10.3.1 (iPhone 6 Plus)

Repro code (it's just a simple navigation, nothing fancy):

// App.xaml.cs
public partial class App : Application
    // public SecondPage CachedPage {get;set;}
    // public SecondPage NoCachedPage => new SecondPage(); 

    public App()
        SecondPage = new Page2();
        MainPage = new NavigationPage(new App4.MainPage());
        var nav = MainPage.Navigation;
// MainPage.xaml.cs
public partial class MainPage : ContentPage
    public MainPage()
        NextPageButton.Clicked += NextPageButton_Clicked;

    private async void NextPageButton_Clicked(object sender, EventArgs e)
        var page = ((App)App.Current).CachedPage; // <-- this throws 
        // var page = ((App)App.Current).NoCachedPage; // <-- this works fine.
        await App.Current.MainPage.Navigation.PushAsync();
// MainPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"

    <Button Text="Next Page" FontSize="Large" x:Name="NextPageButton">

Comment 1 Jimmy [MSFT] 2017-05-17 19:28:13 UTC
Created attachment 22239 [details]
repro project

I was able to reproduce this issue with the project I'm attaching on both iOS and Android. The exception is being thrown from Xamarin.Forms.Core so it's possible this also occurs on Windows in similar scenarios: https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Core/NavigationProxy.cs#L110

Debug output from iOS: https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Core/NavigationProxy.cs#L110

### Steps to Reproduce
1. Run the attached project on iOS or Android (might be easier to repro on an actual device)
2. Press "Next Page"
3. Go back to the previous Page
4. Repeat steps 2 & 3 quickly
Comment 2 Jimmy [MSFT] 2017-05-17 19:29:49 UTC
Link to debug output in comment 1 is wrong. Correct link: https://gist.github.com/jimmgarrido/27755b777af9211ae870eb35a0e6663a
Comment 3 carsten.hagemann 2017-12-12 10:04:51 UTC
Hello, is there any update on this?

We are having this issue when tapping the back button too fast (opening and closing a page basically) and when tapping a list (nested ListViews/ListViewModels) too fast.

A workaround is probably possible, delaying the ClickListener events for 500-1000ms or so, but then the entire UI feels sluggish.
Comment 4 Greg Cadmes 2018-03-12 23:22:52 UTC
Are there any updates or traction on this item?
We're experiencing this issue also and hoping for a quick resolve.