2017-04-03 120 views
-2

我使用後退按鈕下面的代碼,但它不能正常工作不會退出應用,如果我按userdevice的後退按鈕的Android後退按鈕的應用程序沒有關閉

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); 

      } 

     } 
+0

的[點擊返回按鈕兩次退出活動]可能的複製(http://stackoverflow.com/questions/8430805/clicking-the-back-button-twice-to-exit-an-activity ) –

+1

您必須在3秒內按下按鈕2次 – Redman

+0

您可能還有其他活動,關閉其中一個並不意味着關閉應用程序。 – Flot2011

回答

0

改變你這樣的代碼。

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

此解決方案正在工作謝謝 – Dipak

0

使用本...

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); 

     } 

    } 
0

這可能工作。改爲使用finishAffinity()

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); 
    } 
+0

此解決方案正在工作謝謝 – Dipak

0

keep a boolean toClose = false; 和onBackPressed()方法使用此代碼

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); 
    } 
0

這會爲你工作 全球

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. 
    } 
0

定義這一點,使用此功能可以關閉在按下後退按鈕的應用程序。

@Override 
    public void onBackPressed() { 

      Intent intent = new Intent(Intent.ACTION_MAIN); 
      startMain.addCategory(Intent.CATEGORY_HOME); 
      startActivity(intent); 
     } 
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); 

      } 

     } 
相關問題