Search code examples
androidkotlinnavigationfragmentnavigationcontroller

Kill fragment in navigation controller


My flow of fragment is like this

Main -> A -> B -> C ->A

In fragment c, it has a submit button which will return to A. When I press back button in A, I want it back to Main. But it return to fragment c instead.

In fragment C, I use this

 findNavController().navigate(R.id.action_c_to_a)

nav_graph.xml

 <fragment
        android:id="@+id/fragmentC"
        android:name="xxx"
        android:label="xxx">
        <action
            app:launchSingleTop="true"
            app:popUpTo="@+id/fragmentA"
            app:popUpToInclusive="true"
            android:id="@+id/action_c_to_a"
            app:destination="@id/fragmentA" />
    </fragment>

Solution

  • Why not pop up to fragment A? You could just call findNavController().popBackStack(R.id.fragmentA, false) instead of navigating with an action.