Skip to content Skip to sidebar Skip to footer

Prevent Closing By Hardware Back Button In Xamarin Forms On Android

I want to prevent closing the app by pressing the hardware back button in xamarin forms on android. I want, that you can navigate with the hardware back button in the app (what is

Solution 1:

It works with evaluating the NavigationStack (when you use NavigationPage).

In my Activity, I override the OnBackPressed

publicoverridevoidOnBackPressed()
{
    if(App.Instance.DoBack)
    {
        base.OnBackPressed();
    }
}

In my xamarin forms app (App.Instance (it is a singleton)), I will evaluate the NavigationStack of the current Page like this.

public bool DoBack
{
    get
    {
        NavigationPage mainPage = MainPage as NavigationPage;
        if (mainPage != null)
        {
            return mainPage.Navigation.NavigationStack.Count > 1;
        }
        returntrue;                
    }
}

When there is only one page left in the NavigationStack I will not call base.OnBackPressed, so that I will not close the App.

![test]

Solution 2:

And here's what the code could look like for a Xamarin Forms MasterDetail page scenario...

public bool DoBack
    {
        get
        {
            MasterDetailPage mainPage = App.Current.MainPage as MasterDetailPage;

            if (mainPage != null)
            {    
                bool canDoBack = mainPage.Detail.Navigation.NavigationStack.Count > 1 || mainPage.IsPresented;

                // we are on a top level page and the Master menu is NOT showingif (!canDoBack)
                {
                    // don't exit the app just show the Master menu page
                    mainPage.IsPresented = true;
                    returnfalse; 
                }
                else
                {
                    returntrue;
                }                    
            }
            returntrue;
        }
    }

Solution 3:

Just give a blank call in the page where do you wanna prevent, like

protectedoverrideboolOnBackButtonPressed()
{
returntrue;
}

This will prevent the back button in XF-Droid.

Solution 4:

Expanding Chris's answer as there is no App.Instance now and one cannot access App in a static manner within platform code.

1. App.xaml.cs in the Shared project

publicbool DoBack 
{
    get 
    {
        return MainPage.Navigation.NavigationStack.Count > 1;
    }
}

2.MainActivity.cs in the Android project

  • Declare a variable in the class:
App app;
  • In OnCreate(Bundle bundle) change LoadApplication(new App()); to:
app = newApp();
LoadApplication(app);
  • Override the OnBackPressed() method:
publicoverridevoidOnBackPressed()
{
    if (app.DoBack)
    {
        base.OnBackPressed();
    }
}

Solution 5:

Here is a solution that works on Android. Introduce a counter in the application class and increment it with each OnStart and decrement it with each OnStop this way when the counter is 1 you'll know you are at your last activity.

Needless to say, use a base activity implementation so that you don't have to copy-past boiler plate code.

Post a Comment for "Prevent Closing By Hardware Back Button In Xamarin Forms On Android"