Home > Enterprise >  Remove Fragment from backstack with Navigation components
Remove Fragment from backstack with Navigation components

Time:03-30

I have a PinCreateActivity with a navHost fragment with 2 Fragments PinSetup and PinCreate fragments.

When the Activity launches, the PinSetup is the default fragment and after that with a click button i navigate to the PinCreate fragment. What i want is from the PinCreate fragment, when the User clicks the back button NOT to go to the PinSetup and navigate to the backstack as would the PinCreateActivity would do. So i guess when i navigate from the PinSetup to the PinCreate fragment i have to remove the PinSetup from the backStack. How can i do that?

navigation_graph.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@ id/pin_create_nav_graph"
    app:startDestination="@id/pinSetupFragment">

    <fragment
        android:id="@ id/pinSetupFragment"
        android:name="com.example.ui.fragments.pin.PinSetupFragment"
        android:label="Create PIN"
        tools:layout="@layout/fragment_pin_setup" >
        <action
            android:id="@ id/action_pinSetupFragment_to_pinCreateFragment"
            app:destination="@id/pinCreateFragment" />
    </fragment>
    <fragment
        android:id="@ id/pinCreateFragment"
        android:name="com.example.ui.fragments.pin.PinCreateFragment"
        android:label="Your PIN"
        tools:layout="@layout/fragment_pin_create" />
</navigation>

PinCreateActivity

private lateinit var navController: NavController

 override fun onCreate(savedInstanceState: Bundle?) {
 
    ...
    navController = Navigation.findNavController(this, R.id.pin_create_host_fragment)


    // onClick
    navController.navigate(R.id.action_pinSetupFragment_to_pinCreateFragment)

 }

CodePudding user response:

You can do that programmatically from Kotlin code if you need some extra logic while doing pop up. But if you just need to remove PinSetupFragment from your back stack, you can do that on your navigation graph xml file.

So, if you are just going to pop up your fragment without any other extra logic, best way to pop up PinSetupFragment from your back stack would be updating your navigation_graph.xml file.

Just add these two lines to your action:

app:popUpTo="@id/pinSetupFragment"
app:popUpToInclusive="true"

As a result, your navigation_graph.xml file will be like this:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@ id/pin_create_nav_graph"
    app:startDestination="@id/pinSetupFragment">

    <fragment
        android:id="@ id/pinSetupFragment"
        android:name="com.example.ui.fragments.pin.PinSetupFragment"
        android:label="Create PIN"
        tools:layout="@layout/fragment_pin_setup" >
        <action
            android:id="@ id/action_pinSetupFragment_to_pinCreateFragment"
            app:destination="@id/pinCreateFragment"
            app:popUpTo="@id/pinSetupFragment"
            app:popUpToInclusive="true" />
    </fragment>
    <fragment
        android:id="@ id/pinCreateFragment"
        android:name="com.example.ui.fragments.pin.PinCreateFragment"
        android:label="Your PIN"
        tools:layout="@layout/fragment_pin_create" />
</navigation>
  • Related