I used BottomNavigationView
along with NavHostFragment
and I have only 2 tabs at the moment, down below the Navigation Graph I used
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_navigation"
app:startDestination="@id/fragment1">
<fragment
android:id="@+id/fragment1"
android:name="something.ListingFragment"
android:label="@string/hello" />
<fragment
android:id="@+id/fragment2"
android:name="something.ListingFragment2"
android:label="@string/Hi" />
</navigation>
And the BottomNavigation Menu is:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="@id/fragment1"
android:icon="@drawable/ic1"
android:title="@string/hello"
tools:showAsAction="ifRoom" />
<item
android:id="@id/fragment2"
android:icon="@drawable/ic2"
android:title="@string/hi"
tools:showAsAction="ifRoom" />
All setuped together using:
val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_navigation_view)
val navHostFragment = supportFragmentManager.findFragmentById(R.id.navigation_host_fragment) as NavHostFragment
NavigationUI.setupWithNavController(bottomNavigationView, navHostFragment.navController)
Question is, every time I switch between tabs it creates a new instance of the tab fragment I click on, is there a way to manage this to prevent the creation of new items if it already exists in the memory?