Forum Xamarin.Android

Overriding OnBackPressed() method from ContentView codebehind in Xamarin Android?

Hey all, I have a question. I know you can override OnBackPressed from your MainActivity class in an Android Xamarin proyect to change what happens when the user presses the back key. I'd like to override OnBackPressed only for one view, but the method can't be called from the view's codebehind.

I'll explain what I'm trying to do just to make things more clear: I have a ContentPage that emulates page in a recipe book, and you can turn the page or go to the previous page using buttons. This simply does a Navigation.PushASync to a new RecipePage based on a Page object with an id number.

This messes up the navigation stack awfully, since a user can go forwards two pages, then back once, and then forward again, then start pressing the back button on their phone which will make em go through a mess of pages depending on the order they clicked things in. If I can override OnBackPressed I can set the behaviour to simply go back a page with Navigation.PushASync to avoid all the hassle.

I hope the issue is clear with this, let me know if you have any ideas. Thank you in advance!

Best Answer


  • YorkGoYorkGo CNMember, Xamarin Team Xamurai

    @ProgramOtter , have you resolved this issue or is there anything we can help with?

  • ProgramOtterProgramOtter Member ✭✭

    Sorry, I fixed this and had to leave town for a few days. Your answer worked out great! Thank you very much, I hadn't seen this method while browsing microsoft docs.

Sign In or Register to comment.