title: How to keep fragments state like Instagram in Android?
body: <p>I need to implement the UI of my app, like the Instagram one. I need to switch from different fragments, with the usage of the bottom navigation view, but I need to keep state of the fragments, like I left them. How Can I achieve this? </p>
  
  <pre><code>@Override
  public boolean onNavigationItemSelected(@NonNull MenuItem item) {
      item.setChecked(true);
      switch (item.getItemId()) {
          case R.id.action_formation:
              if (homeFragment == null) {
                  homeFragment = new HomeFragment();
              }
              displayFragment(homeFragment);
              break;
          case R.id.action_result:
              if (introResultFragment == null) {
                  introResultFragment = new IntroResultFragment();
              }
              displayFragment(introResultFragment);
              break;
  
          case R.id.action_market:
              displayFragment(new MarketFragment());
              break;
      }
      return false;
  }
  
  
  public void displayFragment(final Fragment fragment) {
      FragmentManager fragmentManager = getFragmentManager();
      FragmentTransaction fragmentTransaction = fragmentManager
              .beginTransaction();
      fragmentTransaction.replace(R.id.container, fragment, fragment.getClass().toString());
      fragmentTransaction.commit();
  }
  </code></pre>