Forum Xamarin.Forms

PopToRootAsync isn't working in my Windows Phone(8.1) project

SubhaMuralidharanSubhaMuralidharan USMember ✭✭
edited July 2015 in Xamarin.Forms

Hi,

I have used PopToRootAsync call in many places in my Xamarin.Forms PCL project which is shared by all the three platforms Android, iOS & WindowsPhone. But PopToRoot method is not working in my Windows Phone project, its working nice in other platforms. In WinPhone it executes the PopToRoot method without any exception, but its not navigating to the root page, it just remains in the same page.

I am not sure what is causing this issue, please someone help me to fix this.

Answers

  • AlexanderSchmitzAlexanderSchmitz USMember
    edited July 2015

    I have the same issue. Did you find any solution or workaround?

    I noticed during debugging that PopToRootAsync actually clears out the whole navigation stack including the root page.
    I tried several workarounds already, e.g. removing pages from the stack with Remove and then call PopAsync, but I always end up with a layout exception afterwards.

  • SubhaMuralidharanSubhaMuralidharan USMember ✭✭
    edited July 2015

    @John.Doe No, not yet. Exactly same here also. Its removing all the pages from the stack, only thing is its not painting the root page. If you find any answer please post it here.

  • I have been trying all kind of tricks, but to no avail yet. If you hook to the event in the NavigationPage:
    NavigationPage navigationPage = new NavigationPage (new MasterPage ()); navigationPage.PoppedToRoot += ( sender, e) => { var page = e.Page; }; MainPage = navigationPage;
    You can see that the current page should be your correct root page. But just like yours my app still shows the old page.
    I will keep trying, but I am running out of ideas.

  • AlexanderSchmitzAlexanderSchmitz USMember
    edited July 2015

    @SubhaMuralidharan
    Well, one of my ideas worked and it is an ugly one. Maybe it works for you too.

    My scenario is that I have a main screen, a detail screen, an edit screen and from the edit screen you can delete the item itself in which case I want to pop back to the main screen jumping over the detail screen. As PopToRootAsync did not work I tried to call PopAsync twice, this resulted in an index out of range exception as the navigation stack is empty as you saw as well. I read in the forums a lot about issues if you have code in OnAppearing. I did not have any code there, but I thought I could chain this the calls, if a page appears and I want to pop to root I just call PopAsync again from the page. This resulted in an exception calculating the layout of controls on the intermediate page, so final attempt: remove page content and then pop.

    So its a chained call of PopAsync through the pages on the navigation stack.

    Long story short, here is the ugliest of workarounds in pseudo code:
    class MainPage { public static bool PoppingToRoot; ... protected override void OnAppearing() { PoppingToRoot = false; } ... }
    class DetailPage1 { ... protected override void OnAppearing() { if (MainPage.PoppingToRoot) { // set content to null to avoid layout exception this.Content = null; // pop Navigation.PopAsync(); } } ... }

    class DetailPage2 { ... private void DoPopToRoot() { MainPage.PoppingToRoot = true; Navigation.PopAsync(); } ... }

  • SubhaMuralidharanSubhaMuralidharan USMember ✭✭

    @John.Doe, Thanks for sharing your solution. I have opened an issue about this in bugzilla.

  • RossPaceRossPace USMember ✭✭

    I've been having the same issue. I have a slightly less ugly solution though:

    var page = Navigation.NavigationStack.First();
    
    while(page.Navigation.NavigationStack.Count > 1)
    {
        await page.Navigation.PopAsync();
    }
    

    Get the root page from the NavigationStack and then loop through calling PopAsync() on the root page's Navigation property. This get's around the out of range exception as when you call PopAsync() on the page you popped it is no longer connected to the NavigationPage and thus has no access to pop more pages.

  • Is someone from Xamarin who can say something about this bug ? I would like to confirm that if you use PopToRootAsync on windows phone 8.1 app and then in normal workflow you would like to use PopAsync then you the app throw exception.

    Solution of RossPace is ok but its ugly on the screen (if you have more views you have to watch all of them back) - it can be temporary solution but not for customers who pay for the apps.

    Xamarin team do something with that

  • MichavanBredaMichavanBreda USMember

    6 months after the last post...
    Just noticed this issue too and I made sure I got all the updates.
    Xamarin, is there any progress on fixing this?

  • RufusLinkeRufusLinke USMember

    I think PopToRootAsync() pops correctly, but doesn't actually activate the remaining root page. Here's my workaround:

    var temporaryRootPage = new Page();
    this.Navigation.InsertPageBefore(temporaryRootPage, this.Navigation.NavigationStack[0]);
    await this.PopToRootAsync();
    await this.PushAsync(desiredRootPage);
    this.Navigation.RemovePage(temporaryRootPage);
    

    It first pushes a temporary page to the first position of the navigation stack, then PopToRootAsync() removes all pages, except this page. PushAsync()ing the page we actually wanted to have as the new root makes it show up. Finally the temporaryRootPage is removed so that our desiredRootPage becomes the root of the stack.

  • PaulDiPietroPaulDiPietro USXamarin Team Xamurai

    This has been fixed and will be in a future release.

  • ganjisganjis USMember

    Hello ,

    Is this released ? I am using Xamarin 4.1 but still Navigation.PopToRootAsync() is not working for both Windows 8.1 and Windows UWP. Can someone please help me to how to solve this ? I tried above two solutions but still not working. Thanks in advance.

  • Jim.5616Jim.5616 USUniversity

    How is this STILL a bug? Can someone from Xamarin PLEASE chime in and let us know WHEN this will be fixed and deployed, and what version??

  • ngamengame USMember ✭✭

    and Still this bug present in UWP and no fix for it since May 2016 that @PaulDiPietro said it fixed

Sign In or Register to comment.