title: Back button not working in fragments
body: <p>I have a <code>MainActivity</code> and some fragments and am using <code>WebView</code> to load some site in all the fragments. Before I wasn't able to go to previous page on press of back button but now I am able to. I have everything working but I can't get a fragment to load on startup. If I add this code in the <code>MainActivity</code> to load a fragment automatically, it works but it breaks the back key navigation in other fragments,</p>
  
  <pre><code>getFragmentManager().beginTransaction()
                  .replace(R.id.content_frame
                          , new aboutus())
                  .addToBackStack(null).commit();
  </code></pre>
  
  <p>Is there any other way by which I can load a fragment by default on startup? Here is my <code>MainActivity</code> (including the code to open fragment automatically) ,</p>
  
  <pre><code>package com.science.s11;
  
  import android.os.Bundle;
  import android.support.design.widget.NavigationView;
  import android.support.v4.view.GravityCompat;
  import android.support.v4.widget.DrawerLayout;
  import android.support.v7.app.ActionBarDrawerToggle;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  import android.view.Menu;
  import android.view.MenuItem;
  
  
  
  public class MainActivity extends AppCompatActivity
      implements NavigationView.OnNavigationItemSelectedListener {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      getFragmentManager().beginTransaction()
              .replace(R.id.content_frame
                      , new aboutus())
              .addToBackStack(null).commit();
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
      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);
  }
  
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
  
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
          return true;
      }
  
      return super.onOptionsItemSelected(item);
  }
  @Override
  public void onBackPressed() {
  
      if (aboutus.mWebView!= null) {
          if (aboutus.mWebView.canGoBack()) {
              aboutus.mWebView.goBack();
          } else {
              if (getFragmentManager().getBackStackEntryCount() &gt; 1) {
                  getFragmentManager().popBackStack();
              }else{
              finish();}
          }
      }
  
      else if(amp.mWebView!= null){
          if ( amp.mWebView.canGoBack()){
              amp.mWebView.goBack();
          }else {
              if (getFragmentManager().getBackStackEntryCount() &gt; 1) {
                  getFragmentManager().popBackStack();
              }else{
                  finish();}
  
          }
      }
      else if(ask.mWebView!= null){
          if ( ask.mWebView.canGoBack()){
              ask.mWebView.goBack();
          }else {
              if (getFragmentManager().getBackStackEntryCount() &gt; 1) {
                  getFragmentManager().popBackStack();
              }else{
                  finish();}
  
          }
      }
      else if(feedback.mWebView!= null){
          if ( feedback.mWebView.canGoBack()){
              feedback.mWebView.goBack();
          }else {
              if (getFragmentManager().getBackStackEntryCount() &gt; 1) {
                  getFragmentManager().popBackStack();
              }else{
                  finish();}
  
          }
      }
      else if(subscribe.mWebView!= null){
          if ( subscribe.mWebView.canGoBack()){
              subscribe.mWebView.goBack();
          }else {
              if (getFragmentManager().getBackStackEntryCount() &gt; 1) {
                  getFragmentManager().popBackStack();
              }else{
                  finish();}
  
          }
      }
      else{
          super.onBackPressed();
      }
  }
  
  @SuppressWarnings("StatementWithEmptyBody")
  @Override
  
  public boolean onNavigationItemSelected(MenuItem item) {
      // Handle navigation view item clicks here.
      int id = item.getItemId();
      android.app.FragmentManager fragmentManager = getFragmentManager();
  
      if (id == R.id.nav_aboutus) {
          fragmentManager.beginTransaction()
                  .replace(R.id.content_frame
                          , new aboutus())
                  .addToBackStack(null).commit();
          // Handle the camera action
      } else if (id == R.id.nav_amp) {
      fragmentManager.beginTransaction()
          .replace(R.id.content_frame
                  , new amp())
              .addToBackStack(null).commit();
      } else if (id == R.id.nav_ask) {
          fragmentManager.beginTransaction()
                  .replace(R.id.content_frame
                          , new ask())
                  .addToBackStack(null).commit();
      } else if (id == R.id.nav_feedback) {
          fragmentManager.beginTransaction()
                  .replace(R.id.content_frame
                          , new feedback())
                  .addToBackStack(null).commit();
      } else if (id == R.id.nav_subscribe) {
          fragmentManager.beginTransaction()
                  .replace(R.id.content_frame
                          , new subscribe())
                  .addToBackStack(null).commit();
      }
  
      DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
      drawer.closeDrawer(GravityCompat.START);
      return true;
      }
  }
  </code></pre>
  
  <p>Here is a sample code which I have in all my fragments</p>
  
  <pre><code>package com.science.s11;
  
  import android.app.Fragment;
  import android.content.Context;
  import android.graphics.Bitmap;
  import android.net.ConnectivityManager;
  import android.os.Bundle;
  import android.view.KeyEvent;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.webkit.WebSettings;
  import android.webkit.WebView;
  import android.webkit.WebViewClient;
  import android.widget.LinearLayout;
  import android.widget.ProgressBar;
  import android.net.NetworkInfo;
  
  public class aboutus extends Fragment {
  
  public static WebView mWebView;
  
  public ProgressBar progressBar;
  public LinearLayout layoutProgress;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
  
      View v = inflater.inflate(R.layout.aboutus, container, false);
      mWebView = (WebView) v.findViewById(R.id.aboutuswebView);
      progressBar = (ProgressBar) v.findViewById(R.id.progressBar);
      layoutProgress = (LinearLayout) v.findViewById(R.id.layoutProgress);
      mWebView.setVisibility(View.GONE);
      mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
      WebSettings webSettings = mWebView.getSettings();
      webSettings.setJavaScriptEnabled(true);
      mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
      WebSettings settings = mWebView.getSettings();
      settings.setSupportMultipleWindows(true);
      settings.setBuiltInZoomControls(false);
      settings.setSupportZoom(false);
      settings.setDisplayZoomControls(false);
  
      mWebView.setWebViewClient(new WebViewClient() {
  
          @Override
          public boolean shouldOverrideUrlLoading(WebView view, String url) {
              view.loadUrl(url);
              return true;
          }
  
  
          @Override
          public void onPageFinished(WebView view, String url) {
              mWebView.setVisibility(View.VISIBLE);
              layoutProgress.setVisibility(View.GONE);
              progressBar.setIndeterminate(true);
              super.onPageFinished(view, url);
  
          }
  
          @Override
          public void onPageStarted(WebView view, String url, Bitmap favicon) {
              layoutProgress.setVisibility(View.VISIBLE);
              progressBar.setIndeterminate(false);
              super.onPageStarted(view, url, favicon);
          }
  
  
      });
  
  
  
  
      if (isOnline()) {
          mWebView.loadUrl("http://science19.com/index.php");
      } else {
          mWebView.loadUrl("file:///android_asset/cti.html");
      }
      return v;
  }
  public boolean onKeyDown(int keyCode, KeyEvent event) {
      if ((keyCode == KeyEvent.KEYCODE_BACK) &amp;&amp; mWebView.canGoBack()) {
          mWebView.goBack();
          return true;
      }
      return onKeyDown(keyCode, event);
  }
  public static boolean canGoBack(){
      return mWebView.canGoBack();
  }
  
  public static void goBack(){
      mWebView.goBack();
  }
  
  
  
  public boolean isOnline() {
      ConnectivityManager cm = (ConnectivityManager)  getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo netInfo = cm.getActiveNetworkInfo();
      return (netInfo != null &amp;&amp; netInfo.isConnected());
  }
  
  }
  </code></pre>
  
  <p>EDIT (added explanation): I actually want to show a fragment on startup of app, the fragment contains WebView. I can do that using this code,</p>
  
  <pre><code>getFragmentManager().beginTransaction()
              .replace(R.id.content_frame
                      , new aboutus())
              .addToBackStack(null).commit();
  </code></pre>
  
  <p>But I even have other fragments and each and everyone has the ability to go back to previous page on back press. Whenever I use this code, I can't go back to previous page on back press on other fragments, it just works in the fragment which is opened using the code, here it is <strong>aboutus</strong>. So, why is this actually happening and how can I fix it?</p>