Bug 28284 - PopModalAsync with MasterDetailPage is clearing the navigation stack in Forms 1.4
Summary: PopModalAsync with MasterDetailPage is clearing the navigation stack in Forms...
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.4.0
Hardware: PC Windows
: Normal major
Target Milestone: ---
Assignee: Rui Marinho
Depends on:
Reported: 2015-03-20 18:40 UTC by Mike
Modified: 2015-09-28 20:15 UTC (History)
7 users (show)

Tags: AC
Is this bug a regression?: ---
Last known good build:

Sample code to demonstrate two breaking issues with Modals (4.39 KB, text/plain)
2015-04-17 12:03 UTC, Phil Morris
Sample working with 1.5.0 (319.10 KB, application/zip)
2015-09-25 07:45 UTC, Rui Marinho

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 on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Mike 2015-03-20 18:40:38 UTC
IF using the MasterDetailPage for the root page, then showing modal windows (PushModalAsync) displays them, when a call to PopModalAsync clears the entire navigation stack instead of just the current modal page. I confirmed that this is new by creating a sample app in 1.3.2 and running it, then upgrading and observing the behavior.

Uploaded code example of issue here: http://benkotips.blob.core.windows.net/public/ModalTest.zip
Comment 1 Phil Morris 2015-04-15 16:29:39 UTC
Also experienced this bug, which is causing major issues with an app that is essential modal popup pages.  Code that worked in Xamarin forms version but fails in version is as follows:

while ( Navigation.ModalStack.Count > Device.OnPlatform(2,2,2) )
    await Navigation.PopModalAsync( false );	
await Navigation.PushModalAsync( new MainPage( ), false );	

Placing a breakpoint in front of the 'await Navigation' code lines and watching the value for ModalStack.Count shows clearly that a single 'pop' sets the modal stack count to zero.

To get my app to work I have to cease popping modal forms.  Not a good idea!
Comment 2 Phil Morris 2015-04-17 01:53:37 UTC
Further information....

When await PopModalAsync() is called, not only does the modal stack count go immediately to zero, but any calls to await PushModalAsync do precisely nothing.  I even tried instantiating a new navigation object after a call to await PopModalAsync then pushing a modal page, to no avail.  Interestingly with version 1.3.x, if I set the parent of the current content page to null, subsequent await PushModalAsync calls also do nothing...
Comment 3 Phil Morris 2015-04-17 12:03:06 UTC
Created attachment 10802 [details]
Sample code to demonstrate two breaking issues with Modals

In a desperate and likely forlorn hope that this issue gets resolved anytime soon, I have attached a single - and simple - source file that demonstrates the two issues discovered regarding Modal popups with Xamarin forms:
1. a call to PopModalAsync sets the ModalStack count to zero, irrespective of the count before making that call, and 
2. any PushModalAsync that immediately follows the PopModalAsync fails to push the selected page.

The sample code has a main page, and three other page (Second, third and fourth).  Actions are very simple

a. On the main page, you can PUSH to the second page.
b. On the second page, you can PUSH to the third page
c. On the third page, you can POP the third page then PUSH to the fourth page
d. On the fourth page you can POP page 4 and PUSH to the main page.

Built with Xamarin Forms 1.3.x, the code works exactly as intended.

Built with 1.4.x, you can NOT get to the forth page!!  The Pop at step c above clears the modal stack count to zero, and the PUSH to the fourth page does nothing.

I cannot re-architect my program to work around both these problems since the app needs to be able to POP back to the previous page at times (impossible because of the first bug).

And I need to use Forms 1.4.x for mobile services such as detecting GPS lat/long values, working with WiFi settings etc.
Comment 4 Rui Marinho 2015-09-24 10:57:48 UTC
Just as information, this works ok with Xamarin Forms Windows Phone 8.1.
Comment 5 Phil Morris 2015-09-24 13:45:58 UTC
Rui - what version of Xamarin Forms did you test with?
Comment 6 Rui Marinho 2015-09-24 16:57:04 UTC
Hey Phil i was trying with the latest version, but after better looking at your sample i saw another problem on the way you were using the Navigation.

You are popping a Page and then using the Navigation property of that popped page to push a new page, and that doesn't work, when you pop the page we clear up that page including that "navigation proxy".
To solve your problem you should reference the page that is kept alive, for example in your sample the NavigationPage that you set in the MainPage, if you change the code for pushing a new page like so it should work:

> await App.Current.MainPage.Navigation.PushModalAsync (new ThirdPage ());
Comment 7 Phil Morris 2015-09-24 17:30:08 UTC
Thanks Rui
Comment 8 Rui Marinho 2015-09-25 07:45:06 UTC
Created attachment 13072 [details]
Sample working with 1.5.0

As for the original issue it seems to be fixed using the latest stable version 1.5.0.
I wasn't able to run the attachment sample provided, but i created a new sample (attached) and i can't reproduce the problem, pushing new modal pages and popping them seems to work correctly
Comment 9 Rui Marinho 2015-09-25 07:45:36 UTC
Comment above
Comment 10 Phil Morris 2015-09-28 19:52:40 UTC

while ( Navigation.ModalStack.Count > Device.OnPlatform( 1, 2, 2 ) )
await Navigation.PopModalAsync( false );																	// remove all pages on iOS, all but the 1st modal on Android
Comment 11 Phil Morris 2015-09-28 20:15:57 UTC
Code that worked in my released app includes the following:

while ( Navigation.ModalStack.Count > Device.OnPlatform( 1, 2, 2 ) )
	await Navigation.PopModalAsync( false );																	// remove all pages on iOS, all but the 1st modal on Android

Each PopModalAsync decrements the modal stack count.  In both Xamarin Forms version 1.4 and 1.5 the PopModalAsync causes the ModalStack.Count to go to zero, so existing code does not work as it did in Forms 1.3

So the problem still exists - I cannot simply pop a certain number of pages off the modal stack; they all get popped with a single call to PopModalAsync.  I did modify code to include 'App.MainPage.' in front of the Navigation object but that made no difference.

My sample code that demonstrates the problem still exhibits the same problem with Forms 1.5.  And although the calls to PushModalAsync() may well require a reference to 'app.MainPage' the more fundamental issue is a single call to  PopModalAsync sets the modal stack count to zero!

I am close to the end of my year's subscription, and have been asked to shell out a not insignificant sum to renew my subscription.  I see no value in doing that if simple code breaks with new releases of Forms.

Does no-one else make use of multiple modal pages???