Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Back Arrow Press Of The NavigationPage in Xamarin Forms

Is there any way to detect the press of the back button of the Navigation Page in Xamarin forms?

like image 366
a.palo Avatar asked Aug 26 '19 17:08

a.palo


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.

like image 144
Nirmal Subedi Avatar answered Sep 18 '22 17:09

Nirmal Subedi


You must override native navigationbar button behavior with custom renderer. OnBackButtonPressed triggers only physical device button. You can read good article how to achive this here

like image 36
Adlorem Avatar answered Sep 17 '22 17:09

Adlorem


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.
like image 20
Adam Diament Avatar answered Sep 20 '22 17:09

Adam Diament