[FIXED] Refresh all navigation stacks to beginning when going to a different shell — Xamarin forms

Issue

I have a couple nav stacks in my xamarin form app like this. The searchPage and aboutPage are both visible/navigable as tabs on the bottom of the screen.

searchPage
    searchResultPage
aboutPage
    settingsPage

I am specifically interested in the case where someone has navigated to search results page (//searchPage/searchResultPage) and then uses the bottom tab to go to the aboutPage. Now that they are in the about page, if they click on the search page tab on the bottom, default behavior is that they’re brought back to the search result page since they never closed out of it. I’d like for this search result page to be closed out automatically so when they click the search page they are indeed brought there (beginning of nav stack) instead of to the search result page (end of nav stack).
TIA!

Edit to add appshell/navigation script–

AppShell:

<TabBar>
        <ShellContent Title="Search" Icon="icon_search.png" Route="SearchPage" ContentTemplate="{DataTemplate local:SearchPage}"/>
        <ShellContent Title="About" Icon="icon_about.png" Route="AboutPage" ContentTemplate="{DataTemplate local:AboutPage}"/>
</TabBar>

Navigation on Search page:

async void LoadSearch(int passedNum)
        {
            await Navigation.PushAsync(new ResultPage(passedNum));
        }

Solution

You can Generate OnDisappearing override in searchResultPage.xaml.cs. Once leaving searchResultPage, it will call OnDisappearing automatically.
In OnDisappearing,the method PopAsync of Navigation pop searchResultPage from the navigation stack. So far, what you need is reached.

protected override void OnDisappearing()
        {
            base.OnDisappearing();
            Navigation.PopAsync();
        }

Answered By – Jianwei Sun

Answer Checked By – Pedro (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published