0

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>
Ahmet Yılmaz
  • 425
  • 5
  • 16
  • Hard to follow your problem, but have a look at this question and the answers given: https://stackoverflow.com/questions/50514758/how-to-clear-navigation-stack-after-navigating-to-another-fragment-in-android This may help you to figure out a solution. – David Wasser Aug 02 '23 at 13:39
  • 1
    Or look at this question: https://stackoverflow.com/questions/60739262/android-navigation-component-clear-stack – David Wasser Aug 02 '23 at 13:41
  • But I'm pretty sure that the standard behaviour for bottom navigation means that you can switch between the views in the bottom nav bar and each view contains its own state and back stack. This is what the user would expect. So maybe you have an architectural confusion. – David Wasser Aug 02 '23 at 13:46
  • Yeah. I think that user this expected. I don't know why this user want this.:D – Ahmet Yılmaz Aug 02 '23 at 13:50
  • You should accept your answer as it will remove the question from the list of unanswered questions and may help someone else with a similar problem. – David Wasser Aug 04 '23 at 09:45
  • @DavidWasser ok I will. – Ahmet Yılmaz Aug 04 '23 at 10:05

1 Answers1

1

@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
                }
            }
        }
Ahmet Yılmaz
  • 425
  • 5
  • 16