Q: Android BottomNavigationView not switching tabs when coming back to the foreground I have an app that uses a BottomNavigationView to navigate between Fragments. 
I've been using this tutorial to keep the fragments in my tabs in memory so they don't need to be recreated every time. It's working pretty well, except for one instance when the app comes back to the foreground. Then the tabs no longer switch. They seem to be switching when I print out the OnNavigationItemSelectedListener, but the tab always remains the same. 
class MainActivity : AppCompatActivity() {

     private lateinit var fragmentManager: androidx.fragment.app.FragmentManager
     lateinit var firstFragment: FirstTabFragment
     lateinit var secondFragment: SecondTabFragment
     lateinit var thirdFragment: ThirdTabFragment
     lateinit var fourthFragment: FourthTabFragment
     lateinit var active: Fragment

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_landing)

        firstFragment = FirstTabFragment()
        secondFragment = SecondTabFragment()
        thirdFragment = ThirdTabFragment()
        fourthFragment = FourthTabFragment()
        active = firstFragment

        fragmentManager = supportFragmentManager

        fragmentManager.beginTransaction().add(R.id.main_content, firstFragment, "1")
        .hide(firstFragment).commit()
        fragmentManager.beginTransaction().add(R.id.main_content, secondFragment, "2")
        .hide(secondFragment).commit()
        fragmentManager.beginTransaction().add(R.id.main_content, thirdFragment, "3")
        .hide(thirdFragment).commit()
        fragmentManager.beginTransaction().add(R.id.main_content, fourthFragment, "4")
        .hide(fourthFragment).commit()

        findViewById<BottomNavigationView(R.id.bottom_navigation)
        .setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener)
        findViewById<BottomNavigationView>(R.id.bottom_navigation).selectedItemId =
        R.id.navigation_tab_1
    }
}

private val mOnNavigationItemSelectedListener =
    BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.navigation_tab_1 -> {
                fragmentManager.beginTransaction().hide(active).show(firstFragment).commit()
                active = firstFragment 

                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_tab_2 -> {
                fragmentManager.beginTransaction().hide(active).show(secondFragment).commit()
                active = secondFragment 

                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_tab_3 -> { 
               fragmentManager.beginTransaction().hide(active).show(thirdFragment).commit()
                active = thirdFragment

                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_tab_4 -> {
                fragmentManager.beginTransaction().hide(active).show(fourthFragment).commit()
                active = fourthFragment

                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }

And here is the Layout that is switching: 
    <LinearLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" />

Would anyone be able to help me fix this issue? 
Github repo for the above code
