title: BottomNavigationView - How to avoid recreation of Fragments and reuse them
body: <p>I would like to make a bottom navigation bar in my project. Every view has it's own fragment. The problem is that every time i click on the button to change the view for example from recents to favorites it creates new fragment with completely new states(e.g. scroll position, text changed whatever my fragment contains). I know that in official Android  documentation there was written that bottom navigation bar should reset the task states, but i think it is too uncomfortable for users.
  I would like to have kind of similar functionality like instagram that you change from feed to explore and back to feed the scroll position the image caches everything remains saved. I tried almost every way to solve this problem the only thing that worked is by setting visibility GONE and setting visibility VISIBLE according to situation but i understand that it is not RIGHT way there should be better way of doing this and i am not talking about manually saving needed instances. I followed almost every tutorial about bottom nav fragments but the interesting thing is that no one is interested to make it use without calling new every time.</p>
  
  <p><a href="https://i.stack.imgur.com/DtUVA.png" rel="noreferrer"><img src="https://i.stack.imgur.com/DtUVA.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/yd5lZ.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/yd5lZ.jpg" alt="enter image description here"></a></p>
  
  <pre><code>FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
  fragmentTransaction.replace(R.id.frameLayout, FirstFragment.newInstance());
  fragmentTransaction.commit();
  
  bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);
  bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
      @Override
      public boolean onNavigationItemSelected(@NonNull MenuItem item) {
          Fragment fragment = null;
          switch (item.getItemId()) {
              case R.id.menu_dialer:
                  fragment = FirstFragment.newInstance();
                  break;
              case R.id.menu_email:
                  fragment = SecondFragment.newInstance();
                  break;
              case R.id.menu_map:
                  fragment = ThirdFragment.newInstance();
                  break;
          }
          if (fragment != null) {
              FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
              fragmentTransaction.replace(R.id.frameLayout, fragment);
              fragmentTransaction.commit();
          }
          return true;
      }
  });
  </code></pre>
  
  <p>I also tried the onAttach and Deattach solutions but again no success.</p>
  
  <p><a href="https://drive.google.com/open?id=0B5Z9TaFoSS7hYVE1OTJtTlA2ajA" rel="noreferrer">VIDEO LINK : new i tried Nino Handler version it only works when i tap on the same fragment button</a></p>
  
  <p><strong>Maybe it is connected that i am using canary version or something wrong in my gradle dependencies?</strong>
  <a href="https://i.stack.imgur.com/3iy1y.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/3iy1y.jpg" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/tmJR4.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/tmJR4.jpg" alt="NEW UPDATES:"></a></p>
  
  <p>NEW UPDATES:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      private TextView mTextMessage;
  
  
      private static final String TAG_FRAGMENT_ONE = "fragment_one";
      private static final String TAG_FRAGMENT_TWO = "fragment_two";
  
      private FragmentManager fragmentManager;
      private Fragment currentFragment;
  
      String TAG = "babken";
      private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
              = new BottomNavigationView.OnNavigationItemSelectedListener() {
  
          Fragment fragment = null;
          @Override
          public boolean onNavigationItemSelected(@NonNull MenuItem item) {
              switch (item.getItemId()) {
                  case R.id.navigation_home:
                     fragment = fragmentManager.findFragmentByTag(TAG_FRAGMENT_ONE);
                      if (fragment == null) {
                          fragment = FragmentFirst.newInstance();
                      }
                      replaceFragment(fragment, TAG_FRAGMENT_ONE);
  
                      break;
                  case R.id.navigation_dashboard:
  
                       fragment = fragmentManager.findFragmentByTag(TAG_FRAGMENT_TWO);
                      if (fragment == null) {
                          fragment = FragmentSecond.newInstance();
                      }
                      replaceFragment(fragment, TAG_FRAGMENT_TWO);
  
                      break;
              }
              return true;
  
          }
      };
  
      private void replaceFragment(@NonNull Fragment fragment, @NonNull String tag) {
          if (!fragment.equals(currentFragment)) {
              fragmentManager
                      .beginTransaction()
                      .replace(R.id.armen, fragment, tag)
                      .commit();
              currentFragment = fragment;
          }
      }
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          fragmentManager = getSupportFragmentManager();
          Fragment fragment = fragmentManager.findFragmentByTag(TAG_FRAGMENT_ONE);
          if (fragment == null) {
              fragment = FragmentFirst.newInstance();
          }
          replaceFragment(fragment, TAG_FRAGMENT_ONE);
          BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
          navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
      }
  
  }
  </code></pre>