2

In my MainActivity, I overrode the OnBackPressed() method like that

public override void OnBackPressed()
{
    Toast toast = Toast.MakeText(this, "Press again to exit", ToastLength.Short);
    toast.SetMargin(0,0.20f);
    toast.Show();
}

When the user is in MainActivity and clicks the back button once, on-screen appears the message "Press again to exit" this message disappears after few seconds. If the user clicks back button again when this message is on screen I want the application to exit but when the message has disappeared and the user clicks back button I want the message to appear again. I've seen some examples doing that in Java, but I find it hard to adapt these examples to work on Xamarin.Android.

FreakyAli
  • 13,349
  • 3
  • 23
  • 63
Profile2ForStack
  • 473
  • 2
  • 8
  • 16

4 Answers4

12

This code works fine.

    long lastPress;

    public override void OnBackPressed()
    {
        // source https://stackoverflow.com/a/27124904/3814729
        long currentTime = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;

        // source https://stackoverflow.com/a/14006485/3814729
        if (currentTime - lastPress > 5000)
        {
            Toast.MakeText(this, "Press back again to exit", ToastLength.Long).Show();
            lastPress = currentTime;
        }
        else
        {
            base.OnBackPressed();
        }
    }

change the time if you want to use short toast length.

ToastLength.Long = 3500 (ms)

ToastLength.Short = 2000 (ms)

Mohamed Krayem
  • 418
  • 8
  • 19
  • 2
    I used this trick on my xamarin forms project, I override the OnBackButtonPressed() of my main page the trick works fine :) – toumir Oct 03 '18 at 15:38
1

The most reliable way I would suggest would be like this :

public static bool doubleBackToExitPressedOnce=true

public override void OnBackPressed()
    {
        if (doubleBackToExitPressedOnce)
        {
            FinishAffinity();
        }
        Common.FragmentManager.doubleBackToExitPressedOnce = true;
        Toast.MakeText(this, Resources.GetString(Resource.String.back_click_on_home), ToastLength.Short).Show();
        new Handler().PostDelayed(new RunnableHelper(), 2000);
    }

 public class RunnableHelper : Java.Lang.Object, IRunnable

 {
    public void Run()
    {
      doubleBackToExitPressedOnce = false;
    }
 }
FreakyAli
  • 13,349
  • 3
  • 23
  • 63
1

Thanks Mohamed Krayem, its work good, but i edited your code to check if this page is a rootPage or not, because the Toast that tell user to click Back button agian to exit app, it appears in all pages not the rootPage only, so i added some lines to code to do that, and it work good for me.

By the way i use this code in Tabbed porject (Shell Application).

    long lastPress;
    public override void OnBackPressed()
    {
        // Check the NavigationStack count if its equal 1 this means that it is on the rootPage
        int NavStackCount = App.Current.MainPage.Navigation.NavigationStack.Count;
        if (NavStackCount == 1)
        {
            long currentTime = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;

            if (currentTime - lastPress > 5000)
            {
                Toast.MakeText(this, "Press back again to exit", ToastLength.Long).Show();
                lastPress = currentTime;
            }
            else
            {
                base.OnBackPressed();
            }
        }
        else
        {
            base.OnBackPressed();
        }
        
    }
Omar Heaba
  • 183
  • 1
  • 12
0
Toast toast = Toast.MakeText(Application.Context, "Press again to exit", ToastLength.Short);
bool doubleBackToExitPressedOnce = false;
public override void OnBackPressed()
{
    if (doubleBackToExitPressedOnce)
    {
        toast.Cancel();
        base.OnBackPressed();
        return;
    }

    toast.SetMargin(0, 0.20f);
    toast.Show();

    this.doubleBackToExitPressedOnce = true;

    new Handler().PostDelayed(() =>
    {
        doubleBackToExitPressedOnce = false;
    }, 2000);

}
Profile2ForStack
  • 473
  • 2
  • 8
  • 16