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
Hi,
You can try overriding the OnOptionsItemSelected method in your MainActivity.
Hope this helps.
Paul Diston
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.
Same problem on iOS...
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
At the end of the method I think if you leave it at the begining that's why the page disappears first.
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?
@DylanLiu override OnBackButtonPressed
@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.
I had to make my own image button on top of it in a custom renderer. Not fun.
Any solutions for this so far ? as in intercepting the navigation bar click and preventing it based on certain conditions ?
Override this on your Page.
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.@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.
If you use
NavigationPage
and add aPage
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.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
And the custom renderer
@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.
This would help. https://theconfuzedsourcecode.wordpress.com/2017/03/12/lets-override-navigation-bar-back-button-click-in-xamarin-forms/
please look at. https://xamarindevelopervietnam.wordpress.com/2016/11/19/how-to-handle-hardwaresoftware-back-button-from-viewmodel-in-xamarin-forms/
Hi,
Please check out this thread to get more insight about Back Button for Navigation bar.
https://theconfuzedsourcecode.wordpress.com/2017/03/12/lets-override-navigation-bar-back-button-click-in-xamarin-forms/comment-page-1/#comment-1167