0

I have 4 fragments: A, B, C and X.

A, B and C are found on the bottom navigation bar, so every time I click on item A, I get the A fragment on the screen, click on B, I get B.

enter image description here

enter image description here

enter image description here

When I click on C, I get the C fragment, which has a button, that takes me to X fragment. Now my problem is, that if I click on the Navigate to X button, after that when I navigate to anywhere from there and then back again on C, I get X.

enter image description here

But everytime I click on the item C, I want to get back to fragment C, no matter if I got to the X fragment before that. How can I implement this?

2 Answers 2

1

I found the answer, I needed to pop the X fragment off the stack, with adding this to the action tag navigating to it:

app:popUpTo='nav_graph_id'

and

app:popUpToInclusive="true"

Sign up to request clarification or add additional context in comments.

Comments

0

@Antal Georgina is correct, Let me elaborate her answer, The app:popUpTo and app:popUpToInclusive attributes are essential when you want to clear fragments or destinations off the navigation stack in Jetpack Navigation.

Here’s a quick recap of what these attributes do:

app:popUpTo: Specifies the destination up to which you want to pop the back stack. The back stack will be cleared up to (but not including) this destination, unless app:popUpToInclusive is set to true.

app:popUpToInclusive="true": If set to true, the destination specified in app:popUpTo will also be removed from the back stack.

Example:

 <action
    android:id="@+id/action_to_fragmentY"
    app:destination="@id/fragmentY"
    app:popUpTo="@id/fragmentX"
    app:popUpToInclusive="true" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.