Is there any other better way to terminate Xamarin Forms App ? Than this ?
In Main Project PCL
MainPage.Xaml.cs(The Page Where i want to Terminate)
protected override bool OnBackButtonPressed()
{
Device.BeginInvokeOnMainThread(async () =>
{
var result = await this.DisplayAlert("Alert!", "want to exit?", "Yes", "No");
if (result)
{
var closer = DependencyService.Get<ICloseApplication>();
closer?.closeApplication();
}
});
return true;
}
Interface
public interface ICloseApplication
{
void closeApplication();
}
Android
[assembly: Xamarin.Forms.Dependency(typeof(CloseApplication))]
namespace XForms.Droid
{
public class CloseApplication : ICloseApplication
{
public void closeApplication()
{
Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
}
}
}
IOS
[assembly: Xamarin.Forms.Dependency(typeof(CloseApplication))]
namespace XForms.Droid
{
public class CloseApplication : ICloseApplication
{
public void closeApplication()
{
Thread.CurrentThread.Abort();
}
}
}