title: Handle back key when the last fragment is popped
body: <p>I add <code>Fragments</code> to my <code>Activity</code> dynamically based on user interaction. When I press the back key, the fragments are popped. However when I press the back key for the fragment which was first added to the stack, the 'Activity' shows an empty layout. I would like the Activity to call `finish()' at this point and disappear. I've tried:</p>
  
  <pre><code>@Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
      super.onKeyDown(keyCode, event);
      if(keyCode == KeyEvent.KEYCODE_BACK){
          if(getFragmentManager().getBackStackEntryCount()==0){
              finish();
              return true;
          }
      }
      return true;
  }
  </code></pre>
  
  <p>But this has the effect of blocking the back key functionality. Any pointers in the right direction are appreciated.</p>