title: How can I stop my fragment from being destroyed?
body: <p>I have a navigation view with some items in it, and when one item is pressed it will go to that fragment. For example, if you press the "Home" item, on the drawer, it will bring up the home fragment. </p>
  
  <pre><code>        navigationView = (NavigationView)findViewById(R.id.navigation_view);
          navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
          @Override
          public boolean onNavigationItemSelected(MenuItem item) {
              switch (item.getItemId()) {
                  case R.id.home_id:
                      HomeFragment homeFragment = (HomeFragment) fragmentManager.findFragmentByTag("Home");
                      if (homeFragment == null)
                          homeFragment = new HomeFragment();
                      fragmentTransaction = fragmentManager.beginTransaction();
                      fragmentTransaction.replace(R.id.Main, homeFragment, "Home");
                      fragmentTransaction.commit();
                      getSupportActionBar().setTitle("Home");
                      item.setChecked(true);
                      drawerLayout.closeDrawers();
                      break;
  
                  case R.id.chat_id:
                      ChatFragment chatFragment = (ChatFragment) fragmentManager.findFragmentByTag("Chat");
                      if (chatFragment == null)
                          chatFragment = new ChatFragment();
                      fragmentTransaction = fragmentManager.beginTransaction();
                      fragmentTransaction.replace(R.id.Main, chatFragment, "Chat");
                      fragmentTransaction.commit();
                      getSupportActionBar().setTitle("Chat");
                      item.setChecked(true);
                      drawerLayout.closeDrawers();
                      break;
              }
  
          }
      }
  </code></pre>
  
  <p>So what I think is happening is that when I am using the <code>replace()</code> method, it is destroying the chat fragment. What if I have data on the chat fragment, and I want to make sure the data does not go away when I change fragments. </p>
  
  <p>In the <code>ChatFragment</code> class I have put this override method:</p>
  
  <pre><code>@Override
  public void onSaveInstanceState(Bundle outState)
  {
      Log.d("CHATFRAG", "onSaveInstanceState: been called ");
      super.onSaveInstanceState(outState);
      outState.putString("textView",textView.getText().toString());
      //Save the fragment's state here
  }
  </code></pre>
  
  <p>Now when I switch between fragments in my app, this method does not get called, which means the fragment is being destroyed. </p>
  
  <p>Is there an alternative way where I can swap the current fragment that is on screen, with the desired fragment(without destroying both)?
  I will be having more fragments (up to ten), is there an optimised solution that will not require duplicate code?</p>