2

let's say I have fragments a->b->c , but "a" is a splash screen, so I want "b" to become first fragment in stack and throw "a" forever, so when I'm i "b" and press "back" system button - I close the app

In SupportFragmentManager I used replace() and that worked. But how can I achieve it here?

1 Answer 1

1

here is an example

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"
app:startDestination="@id/splashFragment">

<fragment
    android:id="@+id/splashFragment"
    android:name="com.example.***.fragments.SplashFragment"
    android:label="SplashFragment"
    tools:layout="@layout/layout_splash">
    <action
        android:id="@+id/action_splashFragment_to_homeFragment"
        app:destination="@id/homeFragment"
        app:popUpTo="@id/splashFragment"
        app:popUpToInclusive="true"/> // here to make home fragment as root
</fragment>
<fragment
    android:id="@+id/homeFragment"
    android:name="com.example.***.fragments.HomeFragment"
    android:label="HomeFragment"
    tools:layout="@layout/layout_home">
</fragment>
</navigation>

and in SplashFragment you just need to navigate to Home in OnCreate()

findNavController().navigate(R.id.action_splashFragment_to_homeFragment)

also if you want to do this in code only

findNavController()
    .navigate(R.id.action_splashFragment_to_homeFragment,
            null,
            NavOptions.Builder()
                    .setPopUpTo(R.id.splashFragment,
                            true).build()
    )
Sign up to request clarification or add additional context in comments.

3 Comments

I have app:popUpToInclusive="true" in splash fragment and still app goes back to it
did you tried to update your navigation library to latest version
important, without app:popUpTo="@id/splashFragment" dont work

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.