I have an activity with two fragments "Home" and "Dashboard". There is a bottom navigation bar, providing two items "Home" and "Dashboar" to navigate to each fragment.
A third fragment, called "Subdashboard" could be called with a button inside the fragment "Dashboard".
Code of MainActivity
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val navView: BottomNavigationView = binding.navView
val navController = findNavController(R.id.nav_host_fragment_activity_main)
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_home, R.id.navigation_dashboard
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
}
Snippet of my button in "DashboardFragment" to navigate to SubDashboardFragment
val mybutton: Button = binding.mybutton
mybutton.setOnClickListener {
findNavController().navigate(R.id.action_navigate_to_subdashboard)
}
My goal : When I am on the Subdashboard fragment, i want to come back to Dashboard Fragment, either by clicking on "Dashboard" item in the menu, either by clicking on the back button on the phone.
I have the following menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/title_home" />
<item
android:id="@+id/navigation_dashboard"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="@string/title_dashboard" />
</menu>
Configuration A
I have the following navigation.xml (with "popUpToInclusive")
<?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/mobile_navigation"
app:startDestination="@+id/navigation_home">
<fragment
android:id="@+id/navigation_home"
android:name="com.example.myapp.HomeFragment"
android:label="Home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/navigation_dashboard"
android:name="com.example.myapp.DashboardFragment"
android:label="Dashboard"
tools:layout="@layout/fragment_dashboard">
<action
android:id="@+id/action_navigate_to_subdashboard"
app:destination="@id/navigation_subdashboard"
app:popUpTo="@id/navigation_dashboard"
app:popUpToInclusive="true"
/>
</fragment>
<fragment
android:id="@+id/navigation_subdashboard"
android:name="com.example.myapp.SubdasboardFragment"
tools:layout="@layout/fragment_subdashboard"
android:label="Subdashboard"/>
</navigation>
Test 1 : When i click on "Dashboard" item, navigation to Dashboard is OK. I click on the button "Go to subdashboard", navigation to Subdashboard is OK. If i click again on the "Dashboard" item in the menu, it's OK, i'm going back to Dashboard Fragment. Behavior is OK.
Test 2 : When i click on "Dashboard" item, navigation to Dashboard is OK. I click on the button "Go to subdashboard", navigation to Subdashboard is OK. If i click on the Back button of the phone, i'm going back directly to Home Fragment (so the start destination). Behavior is not OK
Configuration B
I have the following navigation.xml (without "popUpToInclusive")
<?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/mobile_navigation"
app:startDestination="@+id/navigation_home">
<fragment
android:id="@+id/navigation_home"
android:name="com.example.myapp.HomeFragment"
android:label="Home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/navigation_dashboard"
android:name="com.example.myapp.DashboardFragment"
android:label="Dashboard"
tools:layout="@layout/fragment_dashboard">
<action
android:id="@+id/action_navigate_to_subdashboard"
app:destination="@id/navigation_subdashboard"
app:popUpTo="@id/navigation_dashboard"
/>
</fragment>
<fragment
android:id="@+id/navigation_subdashboard"
android:name="com.example.myapp.SubdasboardFragment"
tools:layout="@layout/fragment_subdashboard"
android:label="Subdashboard"/>
</navigation>
Test 3 : When i click on "Dashboard" item, navigation to Dashboard is OK. I click on the button "Go to subdashboard", navigation to Subdashboard is OK. If i click again on the "Dashboard" item in the menu, i stay stuck on the Subdashboard fragment. Behavior is not OK.
Test 4 : When i click on "Dashboard" item, navigation to Dashboard is OK. I click on the button "Go to subdashboard", navigation to Subdashboard is OK. If i click on the Back button of the phone, i'm going back to Dashboard Fragment, and if i click again on back, i'm going back to Home. Behavior is OK.
Any ideas to resolve this issue ?
Thanks

