-2

i am using the below code for back button but it cant work it does not exit the app if i am pressing the back button of userdevice

  private Boolean exit = false;

        @Override
        public void onBackPressed() {
            if (exit) {
                this.finish(); // finish activity
            } else {
                Toast.makeText(this, "Press Back again to Exit.",
                        Toast.LENGTH_SHORT).show();
                exit = true;
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        exit = false;
                    }
                }, 3 * 1000);

            }

        }
Dipak
  • 1
  • 4

7 Answers7

2

private Boolean exit = false;

        @Override
        public void onBackPressed() {
            if (exit) {
                this.finishAffinity();
            } else {
                Toast.makeText(this, "Press Back again to Exit.",
                        Toast.LENGTH_SHORT).show();
                exit = true;
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        exit = false;
                    }
                }, 3 * 1000);

            }

        }
0

use this...

private long lastBackPressTime = 0;

     @Override
    public void onBackPressed() {

        if (this.lastBackPressTime < System.currentTimeMillis() - 4000) {

            //Add Snhackbar or Toast, whatever you want
            this.lastBackPressTime = System.currentTimeMillis();

        } else {
            if (toast != null) {
                toast.cancel();
            }
            moveTaskToBack(true);

        }

    }
Dhruv Tyagi
  • 814
  • 1
  • 9
  • 28
0

This may work. Use finishAffinity() instead.

  boolean doubleBackToExitPressedOnce = false;

  public void onBackPressed(){
        if(doubleBackToExitPressedOnce)
        {
            finishAffinity();
            return;
        }

        this.doubleBackToExitPressedOnce = true;
        Toast.makeText(this, "Please click back again to exit", Toast.LENGTH_SHORT).show();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                doubleBackToExitPressedOnce = false;

            }
        },3500);
    }
John Joe
  • 12,412
  • 16
  • 70
  • 135
0

keep a boolean toClose=false; and in onBackPressed() method use this code

if (toClose)
    super.onBackPressed();
    else
    {
        toClose=true;
        Utils.showToast(context,getString(R.string.back_again));
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                toClose=false;
            }
        },2000);
    }
Muhib Pirani
  • 765
  • 1
  • 6
  • 15
0

Change Your Code Like This.

 private Boolean exit = false;

            @Override
            public void onBackPressed() {
                if (exit) {
                    this.finishAffinity();
                } else {
                    Toast.makeText(this, "Press Back again to Exit.",
                            Toast.LENGTH_SHORT).show();
                    exit = true;
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            exit = false;
                        }
                    }, 3 * 1000);

                }

            }
0

This will work for you Define this it globally

private boolean doubleBackToExitProceed = false;
@Override
  public void onBackPressed() {
    if (doubleBackToExitProceed) {
      finish();
      return;
    }
    doubleBackToExitProceed = true;
    Toast.makeText(this, "Press Back again to Exit.", Toast.LENGTH_SHORT).show();

    new Handler().postDelayed(new Runnable() {

      @Override
      public void run() {
        doubleBackToExitProceed = false;
      }
    }, 2000);// time in milliseconds.
  } 
Vinod Pattanshetti
  • 2,465
  • 3
  • 22
  • 36
0

Use this to close the app on pressing back button.

@Override
    public void onBackPressed() {

            Intent intent = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startActivity(intent);
        }
Fathima km
  • 2,539
  • 3
  • 18
  • 26