A single way to override the back button behavior
See original GitHub issueDescription
Right now, on both Xamarin Forms and Maui, there is no direct way to override the navbar back button behavior, while you can with phisical/software buttons (on android only) using the OnBackButtonPressed method.
I suggest to add on the Page (or even NavigationPage) a property to Enable or Disable the back button (be it on the navbar or phisical) and an overridable method/event to capture the back button when is pressed (regardless of the type/platform) similar on how someone did in this tutorial
Public API Changes
var page = new ContentPage();
page.IsBackButtonEnabled = false; // It should be a dependency property for it being able to be binded
page.OnBackPressed += BackPressed;
// or overriding it like it is at the moment but also including the navbar back button
// Also, why not a Command like BackPressedCommand to make it work better with MVVM?
Intended Use-Case
Right now you are not able to properly control how the user uses the back button without heavy customization
Issue Analytics
- State:
- Created a year ago
- Reactions:22
- Comments:9
Top Results From Across the Web
Overriding default back button behavior in android
Sounds like you want to override the back button behavior to move the app to the background instead of doing the usual back...
Read more >Provide custom back navigation
Back navigation is how users move backward through the history of screens they previously visited. All Android devices provide a Back button ......
Read more >Overriding Back Button in Xamarin.Forms Shell…
I've been working on trying to override the back button behavior on Xamairn.Forms Shell recently, both with Navigation bar button and Android ...
Read more >Overriding back button in fragments? : r/androiddev
What is the best way to override/force the back button behavior to ... One way would be to make a method OnBackPressed in...
Read more >Overriding onBackPressed() (the Back Button) - YouTube
Learn how to override the onBackPressed() method in Android to require the user to press the back button twice to exit the app, ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@nielstoemen What If Im not using Shell but some other framework? Im checking out Prism.MAUI and for ContentPage I cant get <Shell.BackButtonBehavior> to work. Guess because Im not using Shell. Are developer forced to implement this on their own if not using Shell? 😦
Ah ok. Im not sure if this should go here or to some MAUI discussion but funnsy stuff. I created a class based on NavigationPage and used it in navigation. Then suddenly somehow ContentPage.OnBackButtonPressed started catching Toolbar back button on Android. Huh.
i see, it worked when i posted it. you have to copy paste the link in the browser when you click on it github does weird things. don’t know the reason