title: How to check for existing activities when back button pressed
body: <p>Generally, when the back button is clicked, I call <code>finish()</code>, and I am taken back to the previous <code>activity</code> (which is my MenuActivity) :</p>
  
  <pre><code>@Override
  public void onBackPressed() {
  
      finish();
  
  }
  </code></pre>
  
  <p>However, sometimes the there are no other <code>activities</code> running when the back button is clicked and the app simply exits. Is there a way I can check if an <code>activity</code> exists or is running in the background/in <code>onPause</code> state etc...</p>
  
  <p>I would like to write something like this:</p>
  
  <pre><code>@Override
  public void onBackPressed() {
  
      if(isActivitiesInStack) //if there are still activities in stack
  
          finish(); //simply call finish and be taken back to next activity
  
      else { // else, there are no acitivites in the stack
  
          Intent intent = new Intent(ThisActivity.this, MenuActivity.class); // then create an intent and send me to the menu acitivy
          startActivity(intent);
          finish()          
      }
  }
  </code></pre>