Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

OnBackPressed -> Show Toast ("Press BACK again to exit") -> PostDelay for 2 seconds

Hi, I have almost a year of experience dealing with Java Android language, so I've done the above several times, but now the company I work with expects me to do what I do, in C#... The thing is, I love C# programming, it's very neat and clean, very easy to use, awsome working with .aspx, winforms, console apps, I just love it, but when it comes to Xamarin.Android... -_-... Really man, really a pain everywhere... It's true I'm not the most experienced out there, but still, almost anything I used to do (and easily) in Java, I just can't do in Xamarin, or have to almost invent the wheel all over again to do it... Still, the one thing I really don't do is giving up, hence being here writing this boring-to-read-long-post to ask you awsome people out there, the following:

-> Is there a way (simple or complicated, I don't care) to do this:
- When OnBackPressed() show a Toast (until here all good) and delay the Finish() for 2 seconds (just in case the user pressed back by mistake), and allow for the Finish() to be called if within this to seconds the user pressed back again?

Here's a snippet of what I used to do in Java (I know some of this classes and methods exist in C#, I just can't seem to figure out how I'm going to used them to produce the following):

bool backPressedToExitOnce = false;
public void onBackPressed() {
if (backPressedToExitOnce) {
} else {
this.backPressedToExitOnce = true;
Toast.makeText(this, "Press BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {

        public void run() {
            backPressedToExitOnce = false;
    }, 2000);



  • NickKovalskyNickKovalsky USMember ✭✭✭

    Here you go, but you'd have to google on how to create a Toast for Xamarin, there are plenty of solutions.

            private bool maybe_exit = false;
            protected override bool OnBackButtonPressed()
            //some more custom checks here
                    if (maybe_exit) return false; //QUIT
                    Toast.ShortMessage("Press BACK once again to quit the application");
                    maybe_exit = true;
                    Device.StartTimer(TimeSpan.FromSeconds(2), () =>
                        maybe_exit = false; //reset those 2 seconds
                        // false - Don't repeat the timer 
                        return false;
                    return true; //true - don't process BACK by system
  • NickKovalskyNickKovalsky USMember ✭✭✭

    That was Xamarin.Forms code, not pure android..
    Since you are at Android, more stuff for toast:

        public void ShortAlert(string message)
            var mess = Toast.MakeText(Application.Context, message, ToastLength.Short);
            mess.SetGravity(GravityFlags.Center | GravityFlags.CenterHorizontal, 0, 0);
            //mess.SetGravity(GravityFlags.Bottom | GravityFlags.CenterHorizontal, 0, 0); //as you will
Sign In or Register to comment.