Skip to content Skip to sidebar Skip to footer

Navigation Component Singletask Launching Of Fragment Navigation

I am using Navigation Component to build my application. I would like to implement SingleTask launch mode in the fragment navigation how can I achieve it? For more clarity, I will

Solution 1:

But when the user press the back button from Fragment A it goes to Fragment C, the next back click goes to Fragment B, then the next back click to FRagment A.

If I understand correctly, your Fragment A is the start destination, like a main page, and you want the behavior of "whenever I come back to Fragment A, the next back button I click should exit the app", right?

If so, try to add action from any Fragment to Fragment A and set:

app:popUpTo="@id/fragmentA"app:popUpToInclusive="true" />

It should look like this:

enter image description here

enter image description here

Demo: https://youtu.be/LNyk_FEkZoA

Solution 2:

Posting my working solution, the full navigation script which includes the answer of @Sam Chen

<?xml version="1.0" encoding="utf-8"?><navigationxmlns: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/app_navigation"app:startDestination="@id/fragmentA"><fragmentandroid:id="@+id/fragmentA"android:name="FragmentA"tools:layout="@layout/fragment_a"><actionandroid:id="@+id/action_FragmentA_to_FragmentB"app:destination="@id/dashboardFragment"app:launchSingleTop="true"app:popUpTo="@+id/main_nav_graph"app:popUpToInclusive="true" /></fragment><fragmentandroid:id="@+id/fragmentB"android:name="FragmentB"tools:layout="@layout/fragment_b"><actionandroid:id="@+id/action_FragmentB_to_FragmentC"app:destination="@id/dashboardFragment"app:launchSingleTop="true"app:popUpTo="@+id/main_nav_graph"app:popUpToInclusive="true" /></fragment><fragmentandroid:id="@+id/fragmentC"android:name="FragmentC"tools:layout="@layout/fragment_c"><actionandroid:id="@+id/action_fragemntC_to_fragementA"app:popUpToInclusive="true"app:popUpTo="@id/fragmentA" /></fragment></navigation>

Post a Comment for "Navigation Component Singletask Launching Of Fragment Navigation"