title: Determine backstack fragment empty state in android
body: <p>My application structure look like:</p>
  
  <pre><code>  SplashActivity -&gt; MainActivity -&gt; (switching between many fragments)
  </code></pre>
  
  <p>What I expect:  finish application from main activity when fragment back stack count is zero. Here is my try:</p>
  
  <p><strong>In SplashAcitivty</strong></p>
  
  <pre><code>  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      if (getIntent().getBooleanExtra("exit", false)) {
          finish();
      }
    }
  </code></pre>
  
  <p><strong>In MainActivity</strong>:</p>
  
  <pre><code>@Override
  public void onBackPressed() {
    // I need to implement this method
    if( backstackCount() == 0){
      Intent intent = new Intent(this, SplashActivity.class);
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      intent.putExtra(SplashActivity.EXIT_KEY, true);
      startActivity(intent);
     }else{
      super.onBackPressed();
     }
  }
  </code></pre>
  
  <p>So, please tell me how can I determine the back stack when it 's empty? Because I use <code>SlideMenu</code> library, all of my fragments switch many times, and they are added to back stack when switching. Look like this one:</p>
  
  <pre><code> getSupportFragmentManager()
      .beginTransaction()
      .replace(R.id.content_frame, fragment)
      .addToBackStack(null)
      .commit();
  </code></pre>