Is there any way to detect the press of the back button of the Navigation Page in Xamarin forms?
-
1Please see my answer in this [POST](https://stackoverflow.com/a/62170391/10455239) – Giuseppe Laera Jun 03 '20 at 10:22
-
1Look at this answer I posted https://stackoverflow.com/questions/31696595/how-to-intercept-navigation-bar-back-button-clicked-in-xamarin-forms/63198797#63198797 – jmayor Aug 01 '20 at 16:26
3 Answers
You can override your navigation page "OnBackButtonPressed" method:
protected override bool OnBackButtonPressed()
{
Device.BeginInvokeOnMainThread(async () =>
{
if (await DisplayAlert("Exit?", "Are you sure you want to exit from this page?", "Yes", "No"))
{
base.OnBackButtonPressed();
await App.Navigation.PopAsync();
}
});
return true;
}
If you are using the shell, you can override the Shell's OnNavigating event:
void OnNavigating(object sender, ShellNavigatingEventArgs e)
{
// Cancel back navigation if data is unsaved
if (e.Source == ShellNavigationSource.Pop && !dataSaved)
{
e.Cancel();
}
}
Update: OnBackButtonPressed event will get fired ONLY on Android when user press the Hardware back button.
Seems like you are more interested to implement when any page get disappeared you want to do something! In that case: You have the page's two methods -
protected override void OnAppearing()
{
base.OnAppearing();
Console.WriteLine("Hey, Im coming to your screen");
}
protected override void OnDisappearing()
{
base.OnDisappearing();
Console.WriteLine("Hey, Im going from your screen");
}
You can override those 2 methods on any page to track when they appear and disappear.

- 2,858
- 2
- 22
- 36
-
OnBackButtonPressed never gets called for the ContentPage. Can you let me know the reason for the same? – a.palo Aug 27 '19 at 10:57
-
-
It is inside the navigation page. I can get the page instance from the navigation stack. – a.palo Aug 28 '19 at 07:14
-
This event will raise when the hardware back button is pressed on android. This event is not raised on iOS. – N Subedi Aug 28 '19 at 14:43
-
@a.palo check the updated answer, if that is something you are looking for. – N Subedi Aug 28 '19 at 14:52
-
OnDisappearing is not a good solution, it is call when a modal is displayed over it or the app is backgrounded. – trampster Apr 07 '21 at 04:12
Recent updates to Xamarin forms mean you can now do this in an application made with Shell Navigation for navigation back arrow on both platforms.
Use the Shell.SetBackButtonBehavior method, for example running this code in the constructor of your page object will allow the back navigation to take place only when the bound viewmodel is not busy:
Shell.SetBackButtonBehavior(this, new BackButtonBehavior
{
Command = new Command(async() =>
{
if (ViewModel.IsNotBusy)
{
await Shell.Current.Navigation.PopAsync();
}
})
});
In the body of the Command you can do whatever you need to do when you are intercepting the click of the back button.
- Note that this will affect only the navigation back button, not the Android hardware back button - that will need handling separately as per the answers above. You could write a shared method called from both the back button pressed override and the command on shell back button behaviour places to share the logic.

- 4,290
- 3
- 34
- 55