title: Back button doesn't go back to previous fragment
body: <p>I have a simple android app in which the launcher activity is a navigation drawer activity. I have a couple of fragments that are shown when user clicks any of the sidebar buttons. The activity shows "fragment1" at first. </p>
  
  <pre><code>@Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_menu);
  
          preferences = getSharedPreferences("Settings", 0);
  
          DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
          ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                  this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
          drawer.setDrawerListener(toggle);
          toggle.syncState();
  
          NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
          navigationView.setNavigationItemSelectedListener(this);
  
          categories fragment = new categories();
          FragmentTransaction fragmenttransaction = getSupportFragmentManager().beginTransaction();
          fragmenttransaction.replace(R.id.frame,fragment,"fragment1");
          fragmenttransaction.commit();
  
      }
  </code></pre>
  
  <p>The problem is when I move to other fragments within this activity and tap back button on my device, instead of going back to previous fragment, the app closes immediately.</p>
  
  <p>I have tried some methods like "popbackstack" in onBackPressed function inside activity but that doesn't work as well</p>
  
  <pre><code>  @Override
      public void onBackPressed() {
          DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
          if (drawer.isDrawerOpen(GravityCompat.START)) {
              drawer.closeDrawer(GravityCompat.START);
  
          }
  
          if(getFragmentManager().getBackStackEntryCount() &gt; 0){
              getFragmentManager().popBackStackImmediate();
          }
  
          else{
              super.onBackPressed();
          }
  
      }
  </code></pre>
  
  <p>Fragment Class</p>
  
  <pre><code>public class settings extends Fragment {
  
      SharedPreferences preferences;
      private EditText rangevalue;
      private Button savebtn;
  
      private int range = 0;
   public settings() {
  
      }
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          View v =  inflater.inflate(R.layout.fragment_settings, container, false);
  
          preferences = getActivity().getSharedPreferences("Settings",0);
          rangevalue = (EditText)v.findViewById(R.id.rangeedittext);
          savebtn = (Button)v.findViewById(R.id.savebutton);
  
          rangevalue.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
  
                  range = Integer.parseInt(rangevalue.getText().toString());
              }
          });
  
  
          savebtn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
  
                  if (range == 0)
                  {
                      SharedPreferences.Editor editor = preferences.edit();
                      editor.putInt("Range",2000);
                      editor.apply();
                  }
                  else
                  {
                      SharedPreferences.Editor editor = preferences.edit();
                      editor.putInt("Range",range);
                      editor.apply();
                  }
                  Toast.makeText(getContext(), "Settings Saved", Toast.LENGTH_SHORT).show();
              }
  
          });
  
          return v;
  
      }
  </code></pre>