I am using Android navigation component and BottomNavigationBar for my application.
The Implementation
I have four tabs and i have created a separated navigation graph for each of my tab and 1 navigation graph for authentication related fragments (login_navigation). and my navigation graph looks like this
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_nav_graph"
app:startDestination="@+id/login_navigation">
<include app:graph="@navigation/login_navigation" />
<include app:graph="@navigation/dashboard_navigation" />
<include app:graph="@navigation/documents_navigation" />
<include app:graph="@navigation/templates_navigation" />
<include app:graph="@navigation/settings_navigation" />
</navigation>
My application flow is like this login_navigation -> (if user already authenticated) -> dashboard_navigation, and to open the dashboard i am using this code
findNavController().popBackStack()
findNavController().navigate(R.id.dashboard_navigation)
Problem
If I go from Dashboard -> Documents -> Dashboard it creates a new instance of Dashboard fragment and its viewmodel, and also retain the old instance of Dashboard. If i press back it will go like this Clear the new Dashboard instance -> Documents -> Old Dashboard instance
Expectation Whenever i press Dashboard again, it should not create new instance but it should open the old instance.
How can i achieve this behaviour.

So after a lots of searching and reading documentation, I came up with a solution which is in my opinion pretty neat than my previous implementation, and it solves all of the following problems.
Solution
nav_graph.xml
BottomNavigationView Setup
private fun setupNavigationComponent() { val bottomNavigationView: BottomNavigationView = binding.bottomNavView