Issue
My app is working with the navigation component. In this case, I am have four fragments loading sequence is A->B->C->D. In fragment D, when I finish it all, I wanna go back directly in fragment A and remove fragments B,C,D at the same time. Anyone have solution for this case? Thanks for your respone !
Solution
You can solve the destination situations in the documentation by examining them.
With each navigation action, a destination is added to the back stack. If you were to navigate repeatedly through this flow, your back stack would then contain multiple sets of each destination (A, B, C, A, B, C, A, and so on). To avoid this repetition, you can specify app:popUpTo and app:popUpToInclusive in the action that takes you from destination C to destination A, as shown in the following example:
<fragment
android:id="@+id/c"
android:name="com.example.myapplication.C"
android:label="fragment_c"
tools:layout="@layout/fragment_c">
<action
android:id="@+id/action_c_to_a"
app:destination="@id/a"
app:popUpTo="@+id/a"
app:popUpToInclusive="true"/>
After reaching destination C, the back stack contains one instance of each destination (A, B, C). When navigating back to destination A, we also popUpTo A, which means that we remove B and C from the stack while navigating. With app:popUpToInclusive="true", we also pop that first A off of the stack, effectively clearing it. Notice here that if you don't use app:popUpToInclusive, your back stack would contain two instances of destination A.
Answered By - Arda Kazancı
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)