[FIXED] NavGraph deepLinking

Issue

I have this navigation graph using BottomNavigationView with 2 fragments, Home and Search and both go to Detail fragment

MainGraph.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"
    android:id="@+id/nav_graph"
    app:startDestination="@+id/home">

    <include app:graph="@navigation/home"/>
    <include app:graph="@navigation/search"/>
</navigation>

HomeGraph

<navigation
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/home"
    app:startDestination="@+id/homeScreen">

    <fragment
        android:id="@+id/homeScreen">
        <action
            android:id="@+id/action_home_to_detail"
            app:destination="@id/detailScreen" />
        
    <include app:graph="@navigation/detail"/>

</navigation>

SearchGraph

<navigation
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/search"
    app:startDestination="@+id/searchScreen">

    <fragment
        android:id="@+id/searchScreen">
        <action
            android:id="@+id/action_search_to_detail"
            app:destination="@id/detailScreen" />
        
    <include app:graph="@navigation/detail"/>

</navigation>

DetailGraph

<navigation
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/detail"
    app:startDestination="@id/detailScreen">

    <argument
        android:name="detailID"
        android:defaultValue='""'
        app:argType="string" />

    <fragment
        android:id="@+id/detailScreen"
        tools:layout="@layout/fragment_detail">
       
        <argument
            android:name="detailID"
            android:defaultValue='""'
            app:argType="string" />
      
        <deepLink
            android:id="@+id/deepLinkDetail"
            app:uri="domain.com/detail?id={detailID}" />
    </fragment>
</navigation>

When I click the url, detailScreen is opened well, but findNavController().previousBackStackEntry is null

For now, if previousBackStackEntry is null, I restart the app

    requireActivity().finish()
    requireActivity().startActivity(Intent(requireContext(), MainActivity::class.java))
    requireActivity().finishAffinity()

Thank you very much

Solution

Solved.

I had intent filter in SplashActivity declaration in the manifest instead of the MainActivity

Answered By – Patrick

Answer Checked By – Marilyn (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published