Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How to clear the Navigation Stack in a Xamarin.Forms 4.0+ Shell app?

I like to remove all pages from the navigation stack and make my current page the new root page. How can I do that?
Whole navigation is done with the new Shell navigation (calling Shell.GoToAsync(route) or the Flyout Menu).
I tried collecting the current stack from Shell.Current.CurrentItem.Navigation.NavigationStack and Shell.Current.Navigation.NavigationStack , but the only element in there is null for any reason.
I am using the latest Xamarin 4.2.848062 version.

Best Answer

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited October 2019

    I like to remove all pages from the navigation stack and make my current page the new root page.

    Xamarin.Forms Shell includes a URI-based navigation experience that uses routes to navigate to any page in the application, without having to follow a set navigation hierarchy. In addition, it also provides the ability to navigate backwards without having to visit all of the pages on the navigation stack. So, you cannot get the page from shell's NavigationStack.

    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/shell/navigation

  • thisismyselfthisismyself Member ✭✭

    In addition, it also provides the ability to navigate backwards without having to visit all of the pages on the navigation stack.

    You mean I can go backward anywhere I want cause I can navigate to any URI at any time? But my Android Back Button goes there where he wants, namely down the Navigation Stack.

    So, you cannot get the page from shell's NavigationStack.

    I don't see the relation between this and the quote before. We shouldn't it be possible to remove pages from the Navigation Stack? Why should there be the property Navigation which has the property NavigationStack on the ShellItem?

  • thisismyselfthisismyself Member ✭✭

    Someone has faced the similar problem

    Thank you for that hint, thank for trying to reproduce. I faced a very simliar issue like the reporter, when I tried to switch between two AppShells. I did not expect these two issue could be related. This is the discussion I started about: https://forums.xamarin.com/discussion/comment/391896
    I will definitly keep an eye on the issue you linked.

    Thank you very much.

  • ValentasMatuseviciusValentasMatusevicius LTMember ✭✭

    I have nasty workaround which looks like working.
    First all create your AppBasePage which all your pages should inherit (no big deal)
    Base page should have prop public Action CustomBackButtonAction { get; set; }
    First override android back button behavior in MainActivity. Code of course it not mine

    public override bool OnOptionsItemSelected(IMenuItem item)
            {
                // check if the current item id 
                // is equals to the back button id
                if (item.ItemId == 16908332)
                {
                    // retrieve the current xamarin forms page instance
                    var currentpage = (AppBasePage)
                        Xamarin.Forms.Application.
                            Current.MainPage.Navigation.
                            NavigationStack.LastOrDefault();
    
                    // check if the page has subscribed to 
                    // the custom back button event
                    if (currentpage?.CustomBackButtonAction != null)
                    {
                        // invoke the Custom back button action
                        currentpage?.CustomBackButtonAction.Invoke();
                        // and disable the default back button action
                        return false;
                    }
    
                    // if its not subscribed then go ahead 
                    // with the default back button action
                    return base.OnOptionsItemSelected(item);
                }
                else
                {
                    // since its not the back button 
                    //click, pass the event to the base
                    return base.OnOptionsItemSelected(item);
                }
            }
            public override void OnBackPressed()
            {
                //this is not necessary, but in Android user
                // has both Nav bar back button and
                // physical back button its safe
                // to cover the both events
    
                // retrieve the current xamarin forms page instance
                var currentpage = (AppBasePage)
                    Xamarin.Forms.Application.
                        Current.MainPage.Navigation.
                        NavigationStack.LastOrDefault();
    
                //check if the page has subscribed to
                // the custom back button event
                if (currentpage?.CustomBackButtonAction != null)
                {
                    currentpage?.CustomBackButtonAction.Invoke();
                }
                else
                {
                    base.OnBackPressed();
                }
            }
    

    Then attach this function to that CustomBackButtonAction in AppBasePage. You could do some small casting hacks to invoke it from viewmodel

    public async void GoToRoot()
            {
                var existingPages = Navigation.NavigationStack.ToList();
                foreach (var page in existingPages)
                {
                    if (page == null) continue;
                    Navigation.RemovePage(page);
                }
    
    
                App.Current.MainPage = new AppShell();
            }
    

    I havent seen issues just sometimes user have to click twice back button to go from app.

  • CruelifeCruelife Member
    edited August 30

    I have similar problem that I'm trying to navigate two pages from one to another. Navigation needs to be cleared, to reslove this issue.

    I have a listview on my first page and when I edit the list item and push the "Save" button, that updates the related item on the database. Then, this action (save button click) navigates me to the first page. However on the first page, no item has changed and updated.

    Is there any another solution to refresh the previous page and clear NavigationStack ?

Sign In or Register to comment.