title: Android - Custom back button (back stack)
body: <p>I have implemented my own back stack but I'm not sure what is wrong or what I could improve, my scenario: I have a project with 2 activities, the first one is the "SplashActivity" - where I load some network data - the second one, the MainActivity.</p>
  
  <p>Inside of my MainActivity I have a fragment and inside of this fragment a webview. </p>
  
  <p>The back button should behave like:</p>
  
  <ul>
  <li><p>When the user doesn't navigate inside of my webview, close the app.</p></li>
  <li><p>When the user navigates in webview, use the back history of the browswer.</p></li>
  </ul>
  
  <p>Here is my code:</p>
  
  <pre><code>@Override
     public void onBackPressed() {
  
      Log.d("lastfragment", String.valueOf(fragmentStack.lastElement().getId()));
  
      if (fragmentStack.size() &gt;= 2) {
  
          // implement normal behavior?
  
          Fragment activeFragment=fragmentStack.lastElement();
          FragmentTransaction ft = getFragmentManager().beginTransaction();
          activeFragment.onPause();
          ft.remove(fragmentStack.pop());
  
          Fragment returnToFragment=fragmentStack.lastElement();
          String name = returnToFragment.getClass().getName();
          if(name=="SplashScreen" || name=="LoginFragment"){
  
              // close?
  
          }
          else {
  
              // implement normal behavior?
  
              returnToFragment.onResume();
              ft.show(returnToFragment);
              ft.commit();
          }
      }
      else {
          //close ??
  
      }
  
  }
  </code></pre>