Handling Software and Hardware Back Buttons in MAUI Shell

A guide on handling software (BackButtonBehavior) and hardware (OnBackButtonPressed) back buttons differently in .NET MAUI Shell applications.

The back button can be software-based (in-app button) or hardware-based (OS back key). In MAUI Shell mode, these two types of back buttons are handled differently.

  1. Handling software button
    In the page, bind an event handler to the back button. In this case, the hardware back button will still navigate to the previous page.

    1
    2
    3
    
    <Shell.BackButtonBehavior>
        <BackButtonBehavior Command="{Binding ButtonCommand}"/>
    </Shell.BackButtonBehavior>
    
  2. Handling hardware button
    The hardware back button requires overriding the OnBackButtonPressed method in the page’s .cs file.

    1
    2
    3
    4
    5
    
    protected override bool OnBackButtonPressed()
    {
        // your code
        return base.OnBackButtonPressed();
    }
    
Built with Hugo
Theme Stack designed by Jimmy