title: Clicking on the Android back button twice to exit the app
body: <p>My app contains an initial splash screen, followed by a <code>listview</code> (main activity). Clicking on each row of <code>listview</code> opens each activity. </p>
  
  <p>My requirement is that if we single press the back button from any of my inner activities (activities that are opened when we click the <code>listview</code> rows), it must navigate to my main <code>listview</code>, and then if we press once more from the <code>listview</code> the app must gets closed.</p>
  
  <p>So, if I press the back button from my <code>listview</code> twice it will exit the app correctly. My main problem is that if I press the back button twice from any of my inner activities, my app is not getting closed. I need to press three times, instead of closing the app from any of my inner activities. Can anyone please help me ?</p>
  
  <p>This is my code for exiting the app. I added this code in my main <code>listview</code> class..</p>
  
  <pre><code>private static final int TIME_INTERVAL = 3000; // # milliseconds, desired time passed between two back presses.
  private long mBackPressed;
  
  @Override
  public void onBackPressed()
  {
      if (mBackPressed + TIME_INTERVAL &gt; System.currentTimeMillis()) 
      { 
          super.onBackPressed(); 
          return;
      }
      else { Toast.makeText(getBaseContext(), "Tap back button in order to exit", Toast.LENGTH_SHORT).show(); }
  
      mBackPressed = System.currentTimeMillis();
  }
  } 
  </code></pre>
  
  <p>my <code>manifest.xml</code></p>
  
  <pre><code>&lt;application
      android:allowBackup="true"
      android:label="@string/app_name"
      android:theme="@style/AppTheme"&gt; 
  
      &lt;meta-data android:name="com.google.android.gms.version"
         android:value="@integer/google_play_services_version"/&gt;
  
  
      &lt;activity
          android:name="learnersseries.mathematics.complexnumbers.Firstintro"
           android:screenOrientation="portrait" 
           android:launchMode="singleTop"            
          android:label="@string/app_name" &gt;
  
  
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
  
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
  
  
      &lt;activity android:name="Myintegralpage"
          android:screenOrientation="portrait"
                     &gt;
  
          &lt;intent-filter&gt;&lt;/intent-filter&gt;
      &lt;/activity&gt;
      &lt;activity android:name="myimagine"
          android:screenOrientation="portrait" 
  
         &gt;
          &lt;intent-filter&gt;&lt;/intent-filter&gt;
      &lt;/activity&gt;
      &lt;activity android:name="Myintroductionpage"
          android:screenOrientation="portrait" 
  
        &gt;
          &lt;intent-filter&gt;&lt;/intent-filter&gt;
      &lt;/activity&gt;
      &lt;activity android:name="MainActivity"
          android:noHistory="false"
          android:screenOrientation="portrait" 
         &gt;
  
  
          &lt;intent-filter&gt;&lt;/intent-filter&gt;
      &lt;/activity&gt;
      &lt;activity android:name="Complexnumbers"
          android:screenOrientation="portrait"
           &gt;
  
          &lt;intent-filter&gt;&lt;/intent-filter&gt;
      &lt;/activity&gt;
      &lt;activity android:name="Equality"
          android:screenOrientation="portrait"
           &gt;
  
          &lt;intent-filter&gt;&lt;/intent-filter&gt;
      &lt;/activity&gt;
      &lt;activity android:name="Additionofcomplex"
          android:screenOrientation="portrait"
            &gt;
  
          &lt;intent-filter&gt;&lt;/intent-filter&gt;
      &lt;/activity&gt;
      &lt;activity android:name="Subtraction"
          android:screenOrientation="portrait" 
          &gt;
  
          &lt;intent-filter&gt;&lt;/intent-filter&gt;
      &lt;/activity&gt;
      &lt;activity android:name="multiplication"
          android:screenOrientation="portrait" &gt;
  
          &lt;intent-filter&gt;&lt;/intent-filter&gt;
      &lt;/activity&gt;
      &lt;activity android:name="Division"
          android:screenOrientation="portrait" &gt;
  
          &lt;intent-filter&gt;&lt;/intent-filter&gt;
      &lt;/activity&gt;
      &lt;activity android:name="Conjugate"
          android:screenOrientation="portrait" &gt;
  
          &lt;intent-filter&gt;&lt;/intent-filter&gt;
      &lt;/activity&gt;
      &lt;activity android:name="Modulus"
          android:screenOrientation="portrait" &gt;
  
          &lt;intent-filter&gt;&lt;/intent-filter&gt;
      &lt;/activity&gt;
      &lt;activity android:name="Reciprocal"
          android:screenOrientation="portrait" &gt;
  
          &lt;intent-filter&gt;&lt;/intent-filter&gt;
      &lt;/activity&gt;
      &lt;activity android:name="Square"
          android:screenOrientation="portrait"&gt;
  
  
      &lt;/activity&gt;
      &lt;activity android:name="Representation"
          android:screenOrientation="portrait" &gt;
  
          &lt;intent-filter&gt;&lt;/intent-filter&gt;
      &lt;/activity&gt;
      &lt;activity android:name="Argument"
          android:screenOrientation="portrait" &gt;
  </code></pre>