NavigationStack is going empty after removing one page - Xamarin Forms

KirubaShankarKirubaShankar Member ✭✭
edited September 5 in Xamarin.Forms

MY GOAL: **
We have "switch mode" option in one page (
in page's slide out menu**). On clicking that page, I should remove all the pages behind this page except Root page (Login page).

MY PROBLEM:
Once I remove one page from the stack and at the second looping of for loop, the navigation stack is showing empty. But should contains a list of pages in it.

NOTE:
we will be having this slideout menu in several page and in slideout menu, we will be having "Mode" option, to go to "Switch Mode" page.
So we wont be knowing that how many/what are the pages might be behind this Mode page.

IN SHORT:
WHENEVER I REACH MODE PAGE, ALL THE PAGES BEHIND THIS MODE PAGE SHOULD BE REMOVED (EXCEPT ROOT PAGE)

MY CODE:
var navigationCount = Navigation.NavigationStack.Count;

                        Navigation.PushAsync(new SelectUserPage(string.Empty));

                        for (var count = 1; count < navigationCount; count++)
                        {
                            var page = Navigation.NavigationStack[count];
                            if(page.GetType().Name != "LoginPage")
                                Navigation.RemovePage(page);
                        }

Please help me. Thank you in advance for the helping heart. Needed this badly. :)

Posts

  • KirubaShankarKirubaShankar Member ✭✭

    Below is my updated code:

    var navigationCount = Navigation.NavigationStack.Count;
    
    
                    for (var count = 1; count <= navigationCount; count++)
                    {
                        if (Navigation.NavigationStack.Count == 0)
                            return;
                        var page = Navigation.NavigationStack[navigationCount - count];
                        if (page.GetType().Name != "LoginPage")
                        Navigation.RemovePage(this.Navigation.NavigationStack[navigationCount - count]);
                    }
    
                    Navigation.PushAsync(new SelectUserPage());
    

    Works but the removal of page will be shown to the user.
    First it is removing all the pages except Login page
    and
    it is pushing to new page.

    How to achieve this --> In UI I need to show only Pushing navigation; not popping the page

  • JohnHardmanJohnHardman GBUniversity mod

    @KirubaShankar said:
    **MY GOAL: **
    var navigationCount = Navigation.NavigationStack.Count;

    Navigation.PushAsync(new SelectUserPage(string.Empty));

    for (var count = 1; count < navigationCount; count++)
    {
    var page = Navigation.NavigationStack[count];
    if(page.GetType().Name != "LoginPage")
    Navigation.RemovePage(page);
    }

    Your loop counter/indexing is incorrect.

    Let's say you have two pages in the navigation stack. The first page (at index 0) is the LoginPage, and the last page (at index 1) is your current page (which you want to retain). Your count starts at 1, which is less than 2 (the number of pages in the stack), so the body of your loop is executed. As the page at index 1 is not the LoginPage, you remove it. Unfortunately, that was the page you wanted to keep.

    You have two choices. Either change your loop to:

    for (var count = 0; count < navigationCount - 1; count++)
    

    or change:

    var page = Navigation.NavigationStack[count];
    

    to

    var page = Navigation.NavigationStack[count - 1];
    
  • KirubaShankarKirubaShankar Member ✭✭
    edited September 6

    Hi @JohnHardman
    Thank you for the reply. You are right. I have fixed this yesterday itself.

    I too done a same fix like you said. Below is my code. It works well.

    var navigationCount = Navigation.NavigationStack.Count - 1;
                    for (var count = 1; count <= navigationCount; count++)
                    {
                        if (Navigation.NavigationStack.Count == 0)
                            return;
                        var page = Navigation.NavigationStack[navigationCount - count];
                        if (page.GetType().Name != "LoginPage")
                            Navigation.RemovePage(Navigation.NavigationStack[navigationCount - count]);
                    }
    

    Note: This code should be used in "OnAppearing" of the current page (top page) and not in any constructor (bad practice).

    Thank you again. :smile:

Sign In or Register to comment.