Thursday, March 21, 2013

(Code snippet, Xamarin.Android).

Not sure why I keep posting code snippets.  I have CodeBox now.

I use this as a simple base for my activities when I'm in the Support Library for Xamarin.Android.

public class ActivityBase : FragmentActivity
{
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            // Create your application here
        }
        public override bool OnKeyDown (Keycode keyCode, KeyEvent e)
        {
            if(keyCode == Keycode.Back && e.RepeatCount == 0)
            {
                AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
                alertbox.SetTitle("Are you sure you want to close this Window?");
                alertbox.SetMessage("Close?");
                alertbox.SetPositiveButton("Yes", delegate {
                    Finish();
                });
                alertbox.SetNegativeButton("No", delegate { });
                alertbox.Show();
            }
            return base.OnKeyDown (keyCode, e);
        }
}

People always want a little dialog that pops up and asks them if they are sure they want to back out, before they finish their activity.

No comments:

Post a Comment