Maui Shell app back button

yaron kadman 81 Reputation points
2023-01-05T13:08:09.717+00:00

Hi,
Kind of new to Maui and the Shell concept so I have a basic question.
When using <ShellContent> in the AppShell.xaml I am navigating to the page which I assign using ContentTemplate="{DataTemplate pages:MySecondPage}".
When I get to MySecondPage I do not see a back button and on Android if I press the device's back button the whole app goes to the background.
Is there a built in way to use ContentTemplate but causing it to act like a pushAsync() so that there is a back button?
I guess my other option (if there is no way to do that) is to use menuItem and manage the navigation in code Shell...GoToAsync().
Thanks for any insights.
Yaron

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2023-01-06T03:05:32.76+00:00

    Hello,

    From the bottom principle of the shell, for Flyout or Tabs items, their importance in the shell is considered to be the same, which is considered to be the first page of the program.

    That's why the back key doesn't appear in the upper left corner when you navigate through a Shell Item.

    If you want to enable the back function when navigating the shell items, you could create your own Shell Navigation stack and override some methods to implement this feature.

    Please refer to the following simple code sample:

       private Stack<ShellNavigationState> Uri { get; set; } // Navigation stack.  
       private ShellNavigationState temp; // Prevents applications from adding redundant data to the stack when the back button is clicked.  
       public AppShell()  
       {  
           InitializeComponent();  
           Uri = new Stack<ShellNavigationState>();  
       }  
       protected override void OnNavigated(ShellNavigatedEventArgs args)  
       {  
           base.OnNavigated(args);  
           if (Uri != null && args.Previous != null)  
           {  
               if (temp == null || temp != args.Previous)   
               {  
                   Uri.Push(args.Previous);  
                   temp = args.Current;  
               }  
          }  
       }  
       protected override bool OnBackButtonPressed()  
       {  
           if (Uri.Count > 0)  
           {  
               Shell.Current.GoToAsync(Uri.Pop());  
               return true;  
           }  
           else  
           {  
               return false;  
           }  
       }  
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.