Rakesh Ravi G
Rakesh Ravi G

Reputation: 123

Exit application on OnBackButtonPressed (Xamarin.Forms)

How can we check application is going to be exit in OnBackButtonPressed()

    protected override bool OnBackButtonPressed()
    {
       //Exit form application?
        base.OnBackButtonPressed();
        return false;
    }

Upvotes: 0

Views: 594

Answers (1)

Rakesh Ravi G
Rakesh Ravi G

Reputation: 123

    protected override bool OnBackButtonPressed()
    {
        Device.BeginInvokeOnMainThread(async () =>
        {
            if (await DisplayAlert("", "Exit the application?", "Yes", "No"))
            {
                if (Device.RuntimePlatform == Device.Android)
                {
                    Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
                }
                else if (Device.RuntimePlatform == Device.iOS)
                {
                    Thread.CurrentThread.Abort();
                }
            }
        });
        return true;
    }

Upvotes: 2

Related Questions