PopToRootAsync is not supported globally on Android, please use a NavigationPage.

PaulDistonPaulDiston USUniversity ✭✭✭✭

Hi,

Does anyone have any additional information regarding how I might work around the "PopToRootAsync is not supported globally on Android, please use a NavigationPage." error?

The same code works successfully on Windows Phone.

Thank you very much for your help.

Paul Diston

Posts

  • ChaseFlorellChaseFlorell CAInsider, University mod

    +1 from me on this. When it says "Please use a NavigationPage", what exactly does that mean?

    In my situation, I'm deauthenticating a user, and I need to remove everything from the backstack and re-show the login screen.

  • JamesMontemagnoJamesMontemagno USForum Administrator, Xamarin Team, Developer Group Leader Xamurai

    Have you attempted to make your root a navigationpage?

    In your App.cs do something like:

    return new NavigationPage(new MyFirstPage());

  • KrystianKrystian USMember

    +1 From me too. I've done as @JamesMontemagno‌ suggested but it does't work. My TestApp has 3 pages getMainPage() in App.cs returns Navigation page then rest of pages is type of ContentPage.

  • ChaseFlorellChaseFlorell CAInsider, University mod
    edited June 2014

    So as @JamesMontemagno said, just wrapping your root page in a NavigationPage seems to fix it.

    All I did was change

    // old code - doesn't fix the Navigation limitation of Android
    public static Page GetMainPage() {
        return new MainPage();
    }
    

    to

    // new code, works around the Navigation limitation in Android
    public static INavigation Navigation { get; private set; }
    public static Page GetMainPage() {
        var rootPage = new NavigationPage(new MainPage());
        Navigation = rootPage.Navigation;
        return rootPage;
    }
    

    Now I can use the following methods throughout the app.

    App.Navigation.PushAsync(somePage);
    App.Navigation.PopAsync();
    App.Navigation.PushModalAsync(someModalPage);
    App.Navigation.PopModalAsync();
    App.Navigation.PopToRootAsync();
    
  • KrystianKrystian USMember

    Theres no such property App.Navigation in my case, I'am using Shared Asset Project in my application.

  • ChaseFlorellChaseFlorell CAInsider, University mod
    edited June 2014

    I've defined a Global Navigation object that sits in the App Class

    public class App
    {
        public static INavigation Navigation { get; set; }
        public static Page GetMainPage()
        {
            return new NavigationPage(new MainPage());
        }
    }
    
  • KrystianKrystian USMember

    I've done exactly as you suggested and I'am afraid that it doesn't worked, I've updated every place in my code to use App.Navigation, but still App.Navigation.PopToRootAsync(); doesn't work

  • ChaseFlorellChaseFlorell CAInsider, University mod

    Oh interesting, you appear to be correct ... I just ASSumed that PopToRootAsync would work since the other 4 do work (I don't use PopToRootAsync in my app).

    I just tested this and failed.

  • ChaseFlorellChaseFlorell CAInsider, University mod
    edited June 2014

    Okay, I've been able to create a reproducible example of this failing. I'd like to know if I'm doing this wrong, or if it's a bug.

    Just create a new project.

    Replace your App with this.

    public class App
    {
        public static INavigation Navigation { get; private set; }
    
        public static Page GetMainPage()
        {
             var rootPage = new NavigationPage(new RootPage());
            Navigation = rootPage.Navigation;
            return rootPage;
        }
    }
    

    Create a RootPage.cs and add this

    public class RootPage : ContentPage
    {
        public RootPage()
        {
            var btn = new Button
            {
                Text = "Go To Page 1"
            };
            btn.Clicked += async (sender, args) => await App.Navigation.PushAsync(new Page1());
    
            Content = btn;
        }
    }
    
    public class Page1 : ContentPage
    {
        public Page1()
        {
            var btn = new Button
            {
                Text = "Go To Page 2"
            };
            btn.Clicked += async (sender, args) => await App.Navigation.PushAsync(new Page2());
    
            Content = btn;
        }
    }
    
    public class Page2 : ContentPage
    {
        public Page2()
        {
            var btn = new Button
            {
                Text = "Go To Root"
            };
            btn.Clicked += async (sender, args) => await App.Navigation.PopToRootAsync();
    
            Content = btn;
        }
    }
    

    PushAsync works but PopToRootAsync crashes.

  • ChaseFlorellChaseFlorell CAInsider, University mod

    I've attached a ZIP repro

  • ChaseFlorellChaseFlorell CAInsider, University mod
  • DiogoDiogo USMember

    It happens to me too...

  • JosephSauveJosephSauve USMember ✭✭
    edited June 2014

    Me three.

    This might be one of those platform paradigm things. Android has the concept of navigating around from activity to activity, whereas iOS has a concept of a RootViewController. Maybe the PopToRootAsync() in Xamarin.Forms just isn't well suited to Android. Using one of the other navigation methods may be necessary.

    Although...the documentation for PopToRootAsync() suggests to the developer that their root page will be navigated to.

    "Pops all but the root Page off the navigation stack."
    

    So, yeah, maybe it IS a bug.

  • CraigDunnCraigDunn USXamarin Team Xamurai

    PopToRootAsync behaviour has been filed as a bug.

  • ChaseFlorellChaseFlorell CAInsider, University mod

    I filed it as a bug, and it still listed as new. I'm not sure they've even seen it?

  • IgnacioFuentesIgnacioFuentes USMember

    I am getting the same problem.

  • ChaseFlorellChaseFlorell CAInsider, University mod
  • chris_riesgochris_riesgo USUniversity ✭✭✭

    For the sake of being thorough, I created a full XF repro case. I'm seeing this same behavior on iOS.

    https://github.com/chrisriesgo/repro/tree/master/XF_PopToRootAsync

  • CraigDunnCraigDunn USXamarin Team Xamurai
    edited July 2014

    [Core] navPage.Navigation.PopToRoot[sic] now works as expected is listed as a fix in the latest 1.2.2 prelease 2 beta. I just tested it in one of my samples and it seems to be fixed :)

    Of course, the fix is referring to PopToRootAsync ()

  • chris_riesgochris_riesgo USUniversity ✭✭✭

    @CraigDunn‌ - Thanks! I pulled in that pre-release, and confirmed that PopToRootAsync() works as expected :)

  • MarkErickson.91MarkErickson.91 USMember ✭✭

    What pre-release did you pull? I'm having the same issue. Thanks!

  • ChaseFlorellChaseFlorell CAInsider, University mod
    edited July 2014

    this command should do it for you.

    > Install-Package Xamarin.Forms -pre
    

    Here's the nuget repo

    https://www.nuget.org/packages/Xamarin.Forms/1.2.2.6240-pre2

  • In case it helps anyone, I got this error because I was trying to use PopAsync with something that I pushed via PushModalAsync. Using PopModalAsync fixed the issue.

  • julianMarshalljulianMarshall AUMember

    Thanks for the tip Noah. I was sure I had not used any Modal pops or pushes, but did a quick Ctl-Shft-F for 'Modal' and sure enough there it was. As soon as I removed it everything worked like a bought one. I wish I could get those 3 hours of my life back!

  • KeithRoweKeithRowe USMember
    edited July 2015

    Is it possible the fix for Android caused the Windows Store/Phone 8.1 issue referenced here?

  • ilanolkiesilanolkies ARMember ✭✭

    I have a Modal Page that appears when a button is clickes, and another Modal appears on another click of this Modal.

    In the Main Page:

    var puntajePage = new CargarPuntaje(puntos);
    await Navigation.PushModalAsync(puntajePage);
    

    And in the CargarPuntaje Modal:

    var listado = new ListadoGanadores();
    await Navigation.PushModalAsync(listado);
    

    Then I use this to make the Modals disappear:

    public async void OnButtonClicked(object sender,EventArgs args)
    {
        await Navigation.PopToRootAsync();
    }
    

    But it's not working...

  • Scott_WolfScott_Wolf INMember ✭✭

    @ilanolkies I ran into same scenario here. Got it working?

  • @Scott_Wolf - https://forums.xamarin.com/discussion/22156/poptorootasync-with-modal

    I don't believe PopToRootAsync is meant for modals, but rather the navigation stack.

    If you have a need to pop all modals off the stack, there's a ModalStack property in the Navigation object. I haven't tested this, but I think something like the following could work:

    while (Navigation.ModalStack.Count > 0) { await Navigation.PopModalAsync(); }

    You could presumably wrap that up in an extension method as well:

    public static class NavigationExtensions { public static async Task PopModalToRootAsync(this INavigation navigation) { while (navigation.ModalStack.Count > 0) { await navigation.PopModalAsync(); } } }

    So now you can:
    await Navigation.PopModalToRootAsync();

  • This solution work as i expected on Android

    public static MyMasterDetail RootPage()
    {
        return (MyMasterDetail)Current.MainPage;
    }
    
    public static void NavigateToHomePage()
    {
        try
        {
    
    
            MainPage homePage = new MainPage();
            MyMasterDetail masterDetailRootPage = (MyMasterDetail)Application.Current.MainPage;
            masterDetailRootPage.Detail = new NavigationPage(homePage);
            masterDetailRootPage.IsPresented = false;
    
            Current.MainPage = masterDetailRootPage;
        }
        catch (Exception ex)
        {
            Debug.WriteLine("!!! NavigateToHomePage() Exception !!!");
            Debug.WriteLine("Exception Description: " + ex);
        }
    }
    

    Then you could call from anywhere to go home
    App.NavigateToHomePage();

    Or show masterdetail left side menu
    App.RootPage().IsPresented = true;

  • igorkr_10igorkr_10 Member ✭✭✭

    Hi all. I have issue when using PopToRootAsync and Navigation contains elements only in ModalStack. But everything is ok when ModalStack.Count=0 and NavigationStack.Count>0. Why this method can't work with modal stack?

  • BushbertBushbert Member ✭✭✭

    You need to use popmodalasync to remove your modal page first.

  • igorkr_10igorkr_10 Member ✭✭✭

    @Bushbert said:
    You need to use popmodalasync to remove your modal page first.

    I understand. But my question is: why this method calling as PopToRootAsync but can pop to root only for Navigation Stack?

Sign In or Register to comment.