'Fragment navigation and menu. How to remove latest fragment? Check description

I was incapable of finding an answer for the following context:

Let's say we have a menu and a nav graph instanced in the main activity. We have 3 fragment: Home, ItemList and Settings. We add another fragment called HourPreference. Take the next flow: I press on the bottom menu on the Settings item which takes me on the SettingsFragment. Then we press a button to take us to the HourPreferenceFragment using findNavController().navigate(SettingsFragmentDirections.actionNavigationNotificationsToNavigationHourPreference()). We are on HourPreferenceFragment. IF we press back then it takes us to SettingsFragment. But IF we press on the home item from the bottom menu, then it takes us on that Fragment and when we press on the bottom menu on the Settings item then it directions us to the HourPreferenceFragment and not the SettingsFragment. Code snippets down below. How can we get to the SettingsFragment when we go along with this flow? I appreciate any answer whether you provide me a link to another post, I was just unable to find anything after a couple hours

Briefly:

The behaviour is: Press settings icon (display SettingsFragment) -> Press hour preference (display HourPreferenceFragment)-> press home icon (display HomeFragment) -> press settings icon and it takes me to HourPreference Fragment BUT I want it to take me to the SettingsFragment!

(MainActivity.onCreate):

    val navView: BottomNavigationView = binding.navView

    navController = findNavController(R.id.nav_host_fragment_activity_main)
    val appBarConfiguration = AppBarConfiguration(
        setOf(
            R.id.navigation_home,
            R.id.navigation_items_list,
            R.id.navigation_settings
        )
    )

    setupActionBarWithNavController(navController, appBarConfiguration)
    navView.setupWithNavController(navController)

//also

    override fun onSupportNavigateUp(): Boolean {
          return navController.navigateUp() || super.onSupportNavigateUp()
}

(activity_main.xml)

 <com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/nav_view"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="0dp"
    android:layout_marginEnd="0dp"
    android:background="@color/grey"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/bottom_nav_menu" />

<fragment
    android:id="@+id/nav_host_fragment_activity_main"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="64dp"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toTopOf="@id/nav_view"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:navGraph="@navigation/mobile_navigation" />

Also in the mobile_navigation.xml graph:

    <fragment
    android:id="@+id/navigation_settings"
    android:name="com.example.xpiry1.ui.settings.SettingsFragment"
    android:label="@string/title_settings"
    tools:layout="@layout/fragment_settings" >

    <action
                android:id="@+id/action_navigation_notifications_to_navigation_hour_preference"
        app:destination="@id/navigation_hour_preference"
        app:enterAnim="@anim/nav_default_enter_anim"
        app:popUpTo="@id/navigation_settings" />
    </fragment>

    <fragment
    android:id="@+id/navigation_hour_preference"
    android:name="com.example.xpiry1.ui.settings.preferences.HourPreferenceFragment"
    android:label="Hour Preferences"
    tools:layout="@layout/fragment_hour_preference">
    </fragment>

(SettingsFragment)

 binding.hourPreference.root.setOnClickListener {
            findNavController().navigate(SettingsFragmentDirections.actionNavigationNotificationsToNavigationHourPreference())


Solution 1:[1]

Found the core issue for this problem. The detailed explanation lies in this answer.

For the moment, Is solved it by downgrading the the fragment dependency from 'androidx.navigation:navigation-fragment-ktx:2.4.1' to 'androidx.navigation:navigation-fragment-ktx:2.3.5' and now everything works fine.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 radus14