question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A single way to override the back button behavior

See original GitHub issue

Description

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:open
  • Created a year ago
  • Reactions:22
  • Comments:9

github_iconTop GitHub Comments

3reactions
Pastajellocommented, Jan 7, 2023

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

1reaction
nielstoemencommented, Dec 6, 2022

https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/shell/navigation?view=net-maui-7.0#back-button-behavior

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found