How to intercept Navigation Bar Back Button Clicked in Xamarin Forms?

I have a xamarin forms page where the user can update some data in a form. I need to intercept the Navigation Bar Back Button Clicked to warn the user if some data have not been saved.How to do it?

I'm able to intercept the hardware Bar Back Button Clicked in Android using the Android.MainActivity.OnBackPressed(), but that event is raised only on hardware Bar Back Button Clicked, not on Navigation Bar Back Button Clicked.

I tried also to override Xamarin.Forms.NavigationPageOnBackButtonPressed() but it not works. Why? Any one have already solved that issue?

I also tried by overriding OnDisappear, there are two problems:

The page has already visually disappeared so the "Are you sure?" dialog appears over the previous page.
Cannot cancel the back action.
So,is it possible to intercept the navigation bar back button press?

Thanks for any help on this.

Answers

  • PaulDistonPaulDiston USUniversity ✭✭✭✭

    Hi,

    You can try overriding the OnOptionsItemSelected method in your MainActivity.

    Hope this helps.

    Paul Diston

  • jzeferinojzeferino USUniversity ✭✭

    i have the same issue on iOS. I could do it on android via OnOptionsItemSelected.
    But in Forms iOS whem i press the back button i cant intercep it.

    im trying to do this examples here: http://stackoverflow.com/questions/8228411/detecting-when-the-back-button-is-pressed-on-a-navbar

    but no success. i need to intercep the back button click before the ViewWillDesapear will called.

  • RonFreschiRonFreschi USUniversity ✭✭

    Same problem on iOS...

  • MiguelCervantesMiguelCervantes MXMember ✭✭✭
    edited August 2015

    I think its better to use OnDisapearing in your PCL project.

    OnDisapearing fires when you popAsync() generally this happens when backbutton si pressed. but try to set

    base.OnDisapearing();
    

    At the end of the method I think if you leave it at the begining that's why the page disappears first.

  • DylanLiuDylanLiu USUniversity ✭✭

    base.OnDisapearing(); isn't really suited for what I want to do. For example, I have survey page with a bunch of entry cells. When the user presses the back button, I want to trigger a pop up dialogue that tells the user if he goes back the data that he just entered would be lost. So how do I accomplish this in a Xamarin forms based project?

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    @DylanLiu override OnBackButtonPressed

  • DylanLiuDylanLiu USUniversity ✭✭

    @AlessandroCaliaro the document says OnBackButtonPressed only works for hardware back button for android devices. I'm talking about the back button on the navigation bar.

    Plus, I just found out that when the back button is pressed, the OnAppearing() for the page that is being loaded is called before the OnDisappearing() of the previous page.

  • Shane000Shane000 USMember ✭✭✭

    I had to make my own image button on top of it in a custom renderer. Not fun.

  • SabdullahSabdullah USMember ✭✭
    edited January 2016

    Any solutions for this so far ? as in intercepting the navigation bar click and preventing it based on certain conditions ?

  • TorbenKruseTorbenKruse DEMember ✭✭✭

    You simply can't/shouldn't do that, this was discussed several times. Using the back button is not the only way to navigate back, you can also swipe to go back for example.

    If you don't want the user to navigate back via the default back button use a modal Page instead.

  • AdamPAdamP AUUniversity ✭✭✭✭✭

    @TorbenKruse - but swipe to go back is an additional gesture added. The only way the app can't control going back is via a hardware back button, hence the code. If people code in other ways to go back, they can easily tap them.

    Unless you are talking about leaving the app altogether. If that is the case, then yes I agree not to put this on a first page to prevent leaving the app, it is only used in circumstances such as a page that contains a form that they might want to be reminded to save, instead of losing all their data they just entered, when going back to the previous page within the app.

  • TorbenKruseTorbenKruse DEMember ✭✭✭

    If you use NavigationPage and add a Page to the stack which is not modal, the default behavior, atleast on iOS, lets you swipe to go back to the previous page or use the default back button in the navigation bar. You should not intercept this.

  • NMackayNMackay GBInsider, University ✭✭✭✭✭
    edited January 2016

    You can disable the swipe back gesture in iOS with a customer renderer.

    I'm not saying you should do this but it is an option.

    In MainActivity.cs

    [assembly: ExportRenderer(typeof(NavigationPage), typeof(MyApp.iOS.Renderers.CustomNavigationRenderer))]
    

    And the custom renderer

    using UIKit;
    using Xamarin.Forms.Platform.iOS;
    
    namespace MyApp.iOS.Renderers
    {
        public class CustomNavigationRenderer : NavigationRenderer
        {
            public override void SetViewControllers(UIViewController[] controllers, bool animated)
            {
                base.SetViewControllers(controllers, animated);
    
                foreach (var controller in controllers)
                {
                    ((UINavigationController)controller).InteractivePopGestureRecognizer.Enabled = false;
                }
            }
    
            public override void ViewDidLoad()
            {
                base.ViewDidLoad();
    
                if (InteractivePopGestureRecognizer != null)
                {
                    InteractivePopGestureRecognizer.Enabled = false;
                } 
            }
        }
    }
    
  • SteveShaw.5557SteveShaw.5557 USMember ✭✭

    @MiguelCervantes - two minor points:

    OnDisappearing not a good choice for this question, because he wants to give the user a chance to Cancel the back action. Once OnDisappearing happens, it is too late.

    Second, putting base.OnDisappearing at end of method doesn't change the order of what appears and disappears.

  • batmacibatmaci DEMember ✭✭✭✭
    Isnt ondisapperaing called also when app goes on Background or the Screen is locked on Android? How can i distinguish between backbutton pressed and this behaviour?
Sign In or Register to comment.