title: Close the app when back button clicked twice
body: <p>My Activity flow looked like this</p>
  
  <pre><code>LogIn Activity-&gt; Activity A(Main Page)-&gt;Activity B-&gt; Activity C
  </code></pre>
  
  <p>When button in C is clicked, it will intent to A. </p>
  
  <p>When back button in A is pressed twice,it should close the app.</p>
  
  <pre><code>  boolean doubleBackToExitPressedOnce = false;
       public void onBackPressed(){
              if(doubleBackToExitPressedOnce)
              {
                  super.onBackPressed();
                  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;
                      Intent a = new Intent(Intent.ACTION_MAIN); // close app code
                      a.addCategory(Intent.CATEGORY_HOME);
                      a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                      startActivity(a);
                  }
              },2000);
  </code></pre>
  
  <p>Problem :</p>
  
  <blockquote>
    <p>when I press the button one times, it will display <code>Please click BACK
    again to exit</code>, and then it will close the app automatically even I
    didn't clicked the button twice.</p>
    
    <p>If I click the button twice, it will back to LogIn Activity. What is
    the correct way to write ? Thanks</p>
  </blockquote>
  
  <p><strong>Edit</strong></p>
  
  <p>If I remove the <code>intent</code> </p>
  
  <pre><code> public void onBackPressed(){
          if(doubleBackToExitPressedOnce)
          {
              super.onBackPressed();
              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;
  
              }
          },2000);
  
  
      }
  </code></pre>
  
  <p>When double click the back button, it will back to Activity C again. </p>