Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Jetpack navigation A ->B->C->A

I couldn't find anything online so here I am. I'm using the jetpack navigation component and I want to navigate from fragmentA to fragmentB, and then fragmentB will navigate to fragmentC, but when pressing hw back button I want to go back straight to fragmentA. is this possible with the current release?

like image 868
TootsieRockNRoll Avatar asked Nov 18 '25 03:11

TootsieRockNRoll


1 Answers

This can be done with adding popUpTo to your action where you move from B -> C.

<fragment
    android:id="@+id/fragmentB"
    android:name="com.ballboycorp.anappaday.navigationtest.FragmentB"
    android:label="fragment_b"
    tools:layout="@layout/fragment_b">
    <action
        android:id="@+id/action_fragmentB_to_fragmentC"
        app:destination="@+id/fragmentC"
        app:popUpTo="@+id/fragmentA" />
</fragment>

What that means is

From B move to C and when user clicks back button, move back to A.

You should navigation to C using that action instead of giving destination id.

button.setOnClickListener {
    findNavController().navigate(R.id.action_fragmentB_to_fragmentC)
}

Visually this is how it looks

enter image description here

like image 149
musooff Avatar answered Nov 19 '25 19:11

musooff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!