How to make back button switch to previous tabs in TabbedPage?

Hi, I seem to be stumped on how to do this.

I Have a MasterDetailPage, with a menu as the Master and a TabbedPage wrapped in a NavigationPage as the Detail. I have added several pages as the TabbedPage's children.

When I switch pages by tapping on a tab, I am presented with the new page and everything works... except when I hit the back button. My app just exits instead of going back to a previously selected tab. How can I set this up so that the back button switches the tabs to the previous ones I have tapped on?

Thanks

Best Answer

Answers

  • neptunecenturyneptunecentury USMember ✭✭

    Yeah, I was thinking I could store a list of tabs I selected, then handle the back button, but I was hoping I was just missing something and there was a "Xamarin Way" to do it.

    Ok, I will try it.

  • neptunecenturyneptunecentury USMember ✭✭

    This seems to work. This is what I did:

        /// <summary>
        /// Gets the visited pages
        /// </summary>
        protected Stack<Page> TabStack { get; private set; } = new Stack<Page>();
    
        protected override void OnCurrentPageChanged()
        {
    
            // Get the current page
            var page = CurrentPage;
            if (page != null)
            {
                // Push the page onto the stack
                TabStack.Push(page);
            }
    
            base.OnCurrentPageChanged();            
        }
    
        protected override bool OnBackButtonPressed()
        {
    
            // Go to previous page in the stack. First, pop off the top page since this represents the
            // current page we are on.
            if (TabStack.Any())
            {
                TabStack.Pop();
            }
    
            // See if we have any pages left
            if (TabStack.Any())
            {
                // Pop off the next page and show it
                CurrentPage = TabStack.Pop();
                // Return true to indicate we handled this
                return true;
            }
    
            // We don't have any more pages in the stack so do default
            return base.OnBackButtonPressed();
        }
    
  • ConradHConradH USMember ✭✭
    edited August 9

    @neptunecentury Thanks for this code, it really helped me with a project, however can I suggest the below modification to the OnBackButtonPressed for any future users in my situation, it handles if the current tabbed page has it's own navigation stack to cope with, which our project had.

        protected override bool OnBackButtonPressed()
        {
    
            if (Device.RuntimePlatform == Device.Android)
            {
                // First check if the current tab has any navigation history to work 
               //through before handling the tab pages.
                if (CurrentPage.Navigation.NavigationStack.Count > 1 ||
                    CurrentPage.Navigation.ModalStack.Count > 0)
                {
                    return base.OnBackButtonPressed();
                }
    
                // Go to previous page in the stack. First, pop off the top page since this 
                // represents the current page we are on.
                if (TabStack.Any())
                {
                    TabStack.Pop();
                }
    
                // See if we have any pages left
                if (TabStack.Any())
                {
                    // Pop off the next page and show it
                    CurrentPage = TabStack.Pop();
                    // Return true to indicate we handled this
                    return true;
                } 
            }
    
            // We don't have any more pages in the stack so do default
            return base.OnBackButtonPressed();
    
        }
    
Sign In or Register to comment.