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?


Best Answer


  • 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.

  • 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
        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())
            // 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.