title: Fragments swipe without re-creation and overlapping
body: <p>I saw many similar questions, and even took the answer from one (hide/show), but I found that when you rotate devise or turn on the night mode, embedded in the google experience, the fragment opened at this moment replaces the background of other fragments. How i can fix it? It seems that it appears if we replace <code>view</code> with another fragment instead <code>&lt;fragment&gt;</code>, but I'm not sure about that. Also in the API, I read that there is a <code>setRetainInstance</code> method that saves the fragment when it is rotated, but adding it to <code>onCreateView</code> has not changed anything.</p>
  
  <p>///activity_main</p>
  
  <pre><code>&lt;!-- Fragments Container --&gt;
  
  &lt;include
      android:id="@+id/fragment"
      layout="@layout/activity_main2"
      android:layout_width="0dp"
      android:layout_height="0dp"
      app:layout_constraintBottom_toTopOf="@+id/navigation"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent" /&gt;
  
  &lt;!-- Bottom Navigation View --&gt;
  
  &lt;android.support.design.widget.BottomNavigationView
      android:id="@+id/navigation"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_marginStart="0dp"
      android:layout_marginEnd="0dp"
      android:background="?android:attr/windowBackground"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      app:menu="@menu/bottom_navigation" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>///MainActivity</p>
  
  <pre><code>import android.annotation.SuppressLint;
  import android.content.SharedPreferences;
  import android.os.Bundle;
  import android.support.annotation.NonNull;
  import android.support.constraint.ConstraintLayout;
  import android.support.design.widget.BottomNavigationView;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentActivity;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentTransaction;
  import android.support.v7.app.AppCompatActivity;
  import android.view.MenuItem;
  import android.widget.TextView;
  
  public class MainActivity extends FragmentActivity {
  
      final Fragment fragment1 = new MapsFragment();
      final Fragment fragment2 = new StatsFragment();
      final Fragment fragment3 = new NewsFragment();
      final Fragment fragment4 = new MoreFragment();
      final FragmentManager fm = getSupportFragmentManager();
      private int tabId;
      Fragment active = fragment1;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          loadData();
          BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
          navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
          navigation.setSelectedItemId(tabId);
  
          switch (tabId){
              case R.id.navigation_map:
                  fm.beginTransaction().add(R.id.fragment, fragment4, "4").hide(fragment4).commit();
                  fm.beginTransaction().add(R.id.fragment, fragment3, "3").hide(fragment3).commit();
                  fm.beginTransaction().add(R.id.fragment, fragment2, "2").hide(fragment2).commit();
  
                  fm.beginTransaction().add(R.id.fragment,fragment1, "1").commit();
                  return;
              case R.id.navigation_stats:
                  fm.beginTransaction().add(R.id.fragment, fragment4, "4").hide(fragment4).commit();
                  fm.beginTransaction().add(R.id.fragment, fragment2, "2").hide(fragment2).commit();
                  fm.beginTransaction().add(R.id.fragment, fragment1, "1").hide(fragment1).commit();
  
                  fm.beginTransaction().add(R.id.fragment,fragment3, "3").commit();
                  return;
              case R.id.navigation_news:
                  fm.beginTransaction().add(R.id.fragment, fragment4, "4").hide(fragment4).commit();
                  fm.beginTransaction().add(R.id.fragment, fragment3, "3").hide(fragment3).commit();
                  fm.beginTransaction().add(R.id.fragment, fragment1, "1").hide(fragment1).commit();
  
                  fm.beginTransaction().add(R.id.fragment,fragment2, "2").commit();
                  return;
              case R.id.navigation_more:
                  fm.beginTransaction().add(R.id.fragment, fragment3, "3").hide(fragment3).commit();
                  fm.beginTransaction().add(R.id.fragment, fragment2, "2").hide(fragment2).commit();
                  fm.beginTransaction().add(R.id.fragment, fragment1, "1").hide(fragment1).commit();
  
                  fm.beginTransaction().add(R.id.fragment,fragment4, "4").commit();
                  return;
                  default:
                      fm.beginTransaction().add(R.id.fragment, fragment4, "4").hide(fragment4).commit();
                      fm.beginTransaction().add(R.id.fragment, fragment3, "3").hide(fragment3).commit();
                      fm.beginTransaction().add(R.id.fragment, fragment2, "2").hide(fragment2).commit();
                      fm.beginTransaction().add(R.id.fragment,fragment1, "1").commit();
          }
      }
      private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
              = new BottomNavigationView.OnNavigationItemSelectedListener() {
          @Override
          public boolean onNavigationItemSelected(@NonNull MenuItem item) {
              tabId = item.getItemId();
              //Fragment selectedFragment = null;
              switch (tabId) {
                  case R.id.navigation_map:
                      fm.beginTransaction().hide(active).show(fragment1).commit();
                      active = fragment1;
                      return true;
                  case R.id.navigation_stats:
                      fm.beginTransaction().hide(active).show(fragment2).commit();
                      active = fragment2;
                      return true;
                  case R.id.navigation_news:
                      fm.beginTransaction().hide(active).show(fragment3).commit();
                      active = fragment3;
                      return true;
                  case R.id.navigation_more:
                      fm.beginTransaction().hide(active).show(fragment4).commit();
                      active = fragment4;
                      return true;
              }
              return false;
          }
      };
  
      @SuppressLint("ApplySharedPref")
      private void saveData(){
          SharedPreferences preferences = getSharedPreferences("preferences",MODE_PRIVATE);
          SharedPreferences.Editor editor = preferences.edit();
          editor.putInt("tabId", tabId).commit();
      }
  
      private void loadData(){
          SharedPreferences preferences = getSharedPreferences("preferences",MODE_PRIVATE);
          tabId = preferences.getInt("tabId",0);
      }
  
      @Override
      protected void onStop() {
          saveData();
          super.onStop();
      }
  
      @Override
      protected void onSaveInstanceState(Bundle outState) {
          super.onSaveInstanceState(outState);
          outState.putInt("tabId",tabId);
      }
  
      @Override
      protected void onRestoreInstanceState(Bundle savedInstanceState) {
          super.onRestoreInstanceState(savedInstanceState);
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/v3zhP.png" rel="nofollow noreferrer">overlapping</a></p>