'Navigation between fragments

I decided to move the Bottom Navigation to the Fragment and work with it already there, leaving the Activity completely empty. Direct navigation and findNavController().navigateUp() work without problems. Problems occur when you press the system Back button. At this point, the app_graph back stack is most likely used and the application closes. How to correctly implement the transition when the system back button is pressed? Listen in every fragment?

activity_main:

<androidx.fragment.app.FragmentContainerView 
    android:id="@+id/fragment_container"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:navGraph="@navigation/app_graph" />

fragment_main:

<FrameLayout...>
    <androidx.constraintlayout.widget.ConstraintLayout...>
        <androidx.coordinatorlayout.widget.CoordinatorLayout...>
            <com.google.android.material.appbar.AppBarLayout...>
                <com.google.android.material.appbar.MaterialToolbar... />
                <com.google.android.material.progressindicator.LinearProgressIndicator... />
            </com.google.android.material.appbar.AppBarLayout>

            <androidx.fragment.app.FragmentContainerView
                android:id="@+id/main_fragment_container"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:navGraph="@navigation/main_graph" />

        </androidx.coordinatorlayout.widget.CoordinatorLayout>
        <com.google.android.material.bottomnavigation.BottomNavigationView... />
    </androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

app_graph:

<navigation
    android:id="@+id/app_graph"
    app:startDestination="@id/navigation_start">

    <fragment android:id="@+id/navigation_main"
        android:name="bnpole.rules.presentation.main.MainFragment" />

</navigation>

main_graph:

<navigation
    android:id="@+id/main_graph"
    app:startDestination="@id/navigation_rule_list">

    <fragment android:id="@+id/navigation_rule_list"
        android:name="bnpole.rules.presentation.rule_list.RuleListFragment" >
        <action
            android:id="@+id/action_navigation_rule_list_to_ruleDetailFragment"
            app:destination="@id/ruleDetailFragment" />
    </fragment>
    <fragment
        android:id="@+id/ruleDetailFragment"
        android:name="bnpole.rules.presentation.rule_detail.RuleDetailFragment" >
        <argument
            android:name="id"
            app:argType="string" />
    </fragment>
</navigation>

MainFragment:

val navHostFragment = childFragmentManager.findFragmentById(R.id.main_fragment_container) as NavHostFragment
val navController = navHostFragment.navController
binding.bottomAppBar.setupWithNavController(navController)

RuleListFragment:

val action = RuleListFragmentDirections.actionNavigationRuleListToRuleDetailFragment(id)
findNavController().navigate(action)

I read that the system buttons are container-oriented with app:defaultNavHost="true", but moving this parameter to the container in fragment_main did not give the desired result.



Sources

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

Source: Stack Overflow

Solution Source