title: avoid fragment recreation BottomNavigationView
body: <p>i want to avoid fragment recreation by using BottomNavigationView
  i read about that FragmentTransaction.replace is the problem and changing to add would help, but that didn't work for me..</p>
  <p>maybe you can see here where i'm wrong</p>
  <p>this is my Host Activity which hosting 3 fragments with BottomNavigationView</p>
  <p>'''</p>
  <pre><code>class Host : AppCompatActivity() {
  
  
  
  
  var fragment: Fragment? = null
  
  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_host)
  
      //for hide the actionBar (contains Fragment name) on top of the screen
      val actionBar = supportActionBar
      actionBar?.hide()
  
  
      val navController = Navigation.findNavController(this, R.id.nav_host_fragment)
      val navView = findViewById&lt;BottomNavigationView&gt;(R.id.nav_view)
      navView?.setupWithNavController(navController)
  
  
      NavigationUI.setupWithNavController(navView, navController)
  
      mAdapter = NfcAdapter.getDefaultAdapter(this)
  
      fragmentStayAlive()
  
    
  
  }
  
  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
      super.onActivityResult(requestCode, resultCode, data)
      for (fragment in supportFragmentManager.fragments) {
          fragment.onActivityResult(requestCode, resultCode, data)
      }
  }
  
  
  
  
  fun ByteArray.toHexString() = joinToString(&quot;&quot;) { &quot;%02x&quot;.format(it) }
  
  private fun fragmentStayAlive(){
  
      val fragment1: Fragment = LobbyFragment()
      val fragment2: Fragment = GameSessionFragment()
      val fragment3: Fragment = UserStatusFragment()
      val fm: FragmentManager = supportFragmentManager
      var active = fragment1
  
      fm.beginTransaction().add(R.id.nav_host_fragment, fragment3, &quot;UserStatusFragment&quot;).hide(
          fragment3
      ).commit();
      fm.beginTransaction().add(R.id.nav_host_fragment, fragment2, &quot;GameSessionFragment&quot;).hide(
          fragment2
      ).commit();
      fm.beginTransaction().add(R.id.nav_host_fragment, fragment1, &quot;LobbyFragment&quot;).commit();
  
      val mOnNavigationItemSelectedListener: BottomNavigationView.OnNavigationItemSelectedListener =
          object : BottomNavigationView.OnNavigationItemSelectedListener {
              override fun onNavigationItemSelected(item: MenuItem): Boolean {
                  when (item.getItemId()) {
                      R.id.lobbyFragment -&gt; {
                          fm.beginTransaction().hide(active).show(fragment1).commit()
                          active = fragment1
  
                          return true
                      }
                      R.id.gameSessionFragment -&gt; {
                          fm.beginTransaction().hide(active).show(fragment2).commit()
                          active = fragment2
  
  
                          return true
                      }
                      R.id.userStatusFragment -&gt; {
                          fm.beginTransaction().hide(active).show(fragment3).commit()
                          active = fragment3
  
  
                          return true
                      }
                  }
  
                  replaceFragment(active, null, true, true)
                  return false
  
  
              }
          }
  }
  
  fun replaceFragment(
      fragment: Fragment,
      @Nullable bundle: Bundle?,
      popBackStack: Boolean,
      findInStack: Boolean
  ) {
      val fm = supportFragmentManager
      val ft: FragmentTransaction = fm.beginTransaction()
      val tag = fragment.javaClass.name
  
      val parentFragment: Fragment?
      parentFragment = if (findInStack &amp;&amp; fm.findFragmentByTag(tag) != null) {
          fm.findFragmentByTag(tag)
      } else {
          fragment
      }
      // if user passes the @bundle in not null, then can be added to the fragment
      if (bundle != null) {
          parentFragment!!.arguments = bundle
      } else {
          parentFragment!!.arguments = null
      }
      // this is for the very first fragment not to be added into the back stack.
      if (popBackStack) {
          fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
      } else {
          ft.addToBackStack(parentFragment.javaClass.name + &quot;&quot;)
      }
      ft.add(R.id.nav_view, parentFragment, tag)
      ft.commit()
      fm.executePendingTransactions()
  }
  </code></pre>
  <p>'''</p>
  <p>thanks for all the helpers!</p>