title: Using onBackPressed() in Android Fragments
body: <p>I am working on a project and I need to be able to use the back button in each fragment to navigate between previous fragments, I have methods written to do so by using a back arrow in the action bar, however, I want to be able to use the same functionality on the back button pressed.  I don't want to use the back stack.  Is there a way to do this?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Rather than using the back stack I want to be able to call the go back to previous method below when the user clicks the back button. I need to used the gobackpressed method within fragments. Is this possible? I hope this is clear and concise. Apologies for any confusion caused above.</p>
  
  <p>Go Back to Previous</p>
  
  <pre><code>public void gobackToPreviousFragment(String preFragmentTag, Fragment preFragment){
  
      FragmentManager  fm = getSupportFragmentManager();
  
      FragmentTransaction ft = fm.beginTransaction();
      ft.setCustomAnimations(R.animator.close_slide_in,R.animator.close_slide_out);
  
      ft.show(preFragment);
  
      //**BY REMOVING FRAGMENT, WHEN USER TRIES TO REVISIT, FRAGMENT IS BLACK**
  
      ft.remove(fm.findFragmentByTag(Misc.currentContentFragmentTag));
      ft.addToBackStack(null);
      ft.commit();
  
      Misc.currentContentFragmentTag = preFragmentTag;
  
      createBar(preFragment);
  }
  </code></pre>
  
  <p>Go Forward</p>
  
  <pre><code>public void gotoNextFragment(String nextTag, Fragment nextFragment){
  
      FragmentManager  fm = getSupportFragmentManager();
  
      FragmentTransaction ft = fm.beginTransaction();
      ft.setCustomAnimations(R.animator.enter_slide_in, R.animator.enter_slide_out);
  
      boolean newlyCreated = false;
      if(nextFragment == null){
          nextFragment = Fragment.instantiate(this, nextTag);
          newlyCreated = true;
      }
  
      //hide current fragment
      ft.hide(fm.findFragmentByTag(Misc.currentContentFragmentTag));
  
      if(newlyCreated){
          ft.add(R.id.content_frame, nextFragment, nextTag);
      }
      else{
          ft.show(nextFragment);
      }
  
      ft.addToBackStack(null);
      ft.commit();
      Misc.currentContentFragmentTag = nextTag;
  
      createBar(nextFragment);
  }
  </code></pre>
  
  <p>These are how I navigate back and forth, and I'd like to be able to implement the go back method on the onBackPressed().  Does this make sense?</p>