0

i have a problem with navigation between fragments in Navbar. This is my situation: I have 3 main fragments: Home, List, Settings Home has 2 sub fragment: Add & Edit So, if i navigate between the 3 main fragments with navbar is everything ok, then if i go in one of 2 "sub fragments" and then, (instead of "navigate up" before and then change main) i directly choose one of other 2 "main" i will go there, but then if i come back to List i will see the sub fragment. I have to Navigate Up to see again List. **What i want is **if i switch main fragment when im inside of one of the "subs"(son of List), and then again i choose List, i want see List and not the sub that i left.

My Code:

Navigation


<fragment
    android:id="@+id/homeFragment"
    android:name="com.somi.fidelitycardconnect.ui.home.HomeFragment"
    android:label="Home"
    tools:layout="@layout/fragment_home" />

<fragment
    android:id="@+id/settingsFragment"
    android:name="com.somi.fidelitycardconnect.ui.settings.SettingsFragment"
    android:label="Impostazioni"
    tools:layout="@layout/fragment_settings" />

<fragment
    android:id="@+id/listFragment"
    android:name="com.somi.fidelitycardconnect.ui.list.ListFragment"
    android:label="Lista clienti"
    tools:layout="@layout/fragment_list">
    <action
        android:id="@+id/action_listFragment_to_addFormFragment"
        app:destination="@id/addFormFragment" />

    <action
        android:id="@+id/action_listFragment_to_editFormFragment"
        app:destination="@id/editFormFragment" />

</fragment>

<fragment
    android:id="@+id/addFormFragment"
    android:name="com.somi.fidelitycardconnect.ui.form.AddFormFragment"
    android:label="Aggiungi cliente"
    tools:layout="@layout/fragment_add_form">
</fragment>

<fragment
    android:id="@+id/editFormFragment"
    android:name="com.somi.fidelitycardconnect.ui.form.EditFormFragment"
    android:label="Informazioni cliente"
    tools:layout="@layout/fragment_edit_form">
</fragment>

MainActivity:

binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val navView: BottomNavigationView = binding.navView

        val navController = findNavController(R.id.nav_host_fragment_activity_main)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.homeFragment,
                R.id.listFragment,
                R.id.settingsFragment
            )
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
    }

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.nav_host_fragment_activity_main)
        return navController.navigateUp(appBarConfiguration)
                || super.onSupportNavigateUp()
    }
}

enter image description here enter image description here

**What i want is **if i switch main fragment when im inside of one of the "subs"(son of List), and then again i choose List, i want see List and not the sub that i left.

1 Answers1

0

If you want to press one of the bottomNav items and start with the default position, you need to pop the other destinations. Use below code to achieve it:

        navView.setupWithNavController(navController)

        navView.setOnItemSelectedListener { item ->
            NavigationUI.onNavDestinationSelected(item, navController)
            navController.popBackStack(item.itemId, inclusive = false)
            true
        }
Mert
  • 904
  • 4
  • 9
  • 21