T
T
Tsuzukeru2021-02-21 18:52:08
Android
Tsuzukeru, 2021-02-21 18:52:08

How to overload navHostFragment with navigation component?

There is an application consisting of two fragments.

<?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/app_navigation"
    app:startDestination="@id/mediaFragment">
    <fragment
        android:id="@+id/mediaFragment"
        android:name="com.nasa.app.ui.media_preview.PreviewMediaFragment"
        android:label="fragment_media"
        tools:layout="@layout/fragment_media_preview" >
        <action
            android:id="@+id/action_mediaFragment_to_detailMediaFragment"
            app:destination="@id/detailMediaFragment" />
    </fragment>

    <fragment
        android:id="@+id/detailMediaFragment"
        android:name="com.nasa.app.ui.media_detail.DetailMediaFragment"
        android:label="DetailMediaFragment" >
        <argument
            android:name="nasaId"
            app:argType="string" />
        <argument
            android:name="contentType"
            app:argType="com.nasa.app.data.model.ContentType" />
    </fragment>
</navigation>


Is it possible with the navigation component to clear the entire backstack and reload the navHostFragment to the top?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tsudzukeru, 2021-02-21
@Tsuzukeru

val navController = findNavController(R.id.nav_host_fragment)
                val navHostFragment =supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
                val inflater = navHostFragment.navController.navInflater
                val graph = inflater.inflate(R.navigation.app_navigation)
                graph.startDestination = R.id.mediaFragment
                navController.graph = graph

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question