Search code examples
androidkotlinandroid-fragmentsandroid-navigation

How to prevent to reopen fragment in Main fragmet of BottomNavigation?


I have 5 item in BottomNavigationBar inside MainActivity class. When open EarningFragment it has FragmentContairView and it open MoneyDetailFragment from EarningFragment. When I opened this MoneyDetailFragment it opened.

In the scenario when click another item on BottomNavigationBar and click again item for EarningFragment it rememeber that was in MoneyDetailFragment and it opened this class. I don't want it is remember where it was. I want it open only EarningFragment when click it in BottomNavigationBar. That's so complicated hope you can understand it.

MainActivity.kt

class MainActivity : AppCompatActivity(), NavController.OnDestinationChangedListener,
                     View.OnClickListener {
private lateinit var navHostFragment: NavHostFragment

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
navHostFragment =
            supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
 navHostFragment.navController.addOnDestinationChangedListener(this)
}
     private fun setupBottomNavigationView() {
     BottomNavigationViewHelper.setupBottomNavigationView(binding.bottomBar.bottomNavViewBar)
     binding.bottomBar.bottomNavViewBar.setupWithNavController(navHostFragment.navController)
        }
}

nav_graph_main.xml

    <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/nav_graph_main"
        app:startDestination="@id/miHome">
    
        <fragment
            android:id="@+id/miHome"
            android:name="com.mi.ui.home.HomeFragment"
            android:label="@string/menu_home"
            tools:layout="@layout/fragment_home">
            <action
                android:id="@+id/action_to_earningFragment"
                app:destination="@id/miEarnings" />
            <action
                android:id="@+id/action_to_moneyGoldRegisterFragment"
                app:destination="@id/moneyGoldRegisterFragment" />
</fragment>
    <fragment
        android:id="@+id/miEarnings"
        android:name="com.mi.ui.earning.EarningFragment"
        android:label="@string/menu_bottom_earnings"
        tools:layout="@layout/fragment_earning">
        <action
            android:id="@+id/action_to_moneyCardFragment"
            app:destination="@id/moneyCardFragment" />
        <action
            android:id="@+id/action_to_moneyDetailFragment"
            app:destination="@id/moneyDetailFragment" />
    </fragment>

Solution

  • @DavidWasser thank you so much. The user as "Biscuit" named in the second link that I solved from it. I added these code to MainActivity to clear state of navigation that I want to open.

     binding.bottomBar.bottomNavViewBar.setOnItemSelectedListener {
                when (it.itemId) {
                    R.id.miHome -> {
                        navHostFragment.navController.navigate(R.id.miHome)
                        true
                    }
                    R.id.miBarcode -> {
                        navHostFragment.navController.navigate(
                            R.id.miBarcode,
                            null,
                            NavOptions.Builder()
                                .setPopUpTo(R.id.miBarcode, true)
                                .build())
                        true
                    }
                    R.id.miEarnings -> {
                        navHostFragment.navController.navigate(
                            R.id.miEarnings,
                            null,
                            NavOptions.Builder()
                                .setPopUpTo(R.id.miEarnings, true)
                                .build())
                        true
                    }
                    R.id.miProfil -> {
                        navHostFragment.navController.navigate(
                            R.id.miProfil,
                            null,
                            NavOptions.Builder()
                                .setPopUpTo(R.id.miProfil, true)
                                .build())
                        true
                    }
                    else -> {
                        navHostFragment.navController.navigate(R.id.miHome)
                        true
                    }
                }
            }