Double tap to exit in xamarin Android

So I had to implement double tap to exit from app in Android using xamarin. This how it is implemented

  • Create blank Android App

create project

 

  • Override OnBackPressed method in MainActivity.cs
private bool doubleBackToExitPressedOnce = false;
public override void OnBackPressed()
{
if (doubleBackToExitPressedOnce)
{
base.OnBackPressed();
Java.Lang.JavaSystem.Exit(0);
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.MakeText(this, "Double tap to exit", ToastLength.Short).Show();
new Handler().PostDelayed(() =>
{
doubleBackToExitPressedOnce = false;
}, 2000);
}

view raw
MainActivity.cs
hosted with ❤ by GitHub

  • That’s it 🙂 Run the app and see the out put

double tap to exit

GIF_Maker_VideoToGif_15-3-2018_95808_PM

Thanks. 🙂

 

3 thoughts on “Double tap to exit in xamarin Android

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s