Deeplink isn't correctly redirect if app is closed

Viewed 584

I'm using Jetpack Navigation for deeplinks. Deeplink isn't redirect correctly if the app wasn't opened before. However, if the app exists in memory, the app is coming to the foreground and deeplink is opening correctly. I have 2 activity. One registerActivity the other mainActivity. I want to use deeplinks in fragments in mainActivity.

Is it a bug in Jetpack Navigation or I forgot about something when setting it up?

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.life4.ecommerce">


<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<application
    android:name=".ECommerceApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.ECommerce"
    android:requestLegacyExternalStorage="true"
    android:usesCleartextTraffic="true">

    <!--
         The API key for Google Maps-based APIs is defined as a string resource.
         (See the file "res/values/google_maps_api.xml").
         Note that the API key is linked to the encryption key used to sign the APK.
         You need a different API key for each encryption key, including the release key that is used to
         sign the APK for publishing.
         You can define the keys for the debug and release targets in src/debug/ and src/release/.
    -->
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="@string/google_maps_key" />

    <activity android:name=".view.RegisterActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".view.MainActivity"
        android:windowSoftInputMode="adjustResize">
        <nav-graph android:value="@navigation/nav_main" />
    </activity>
</application>

</manifest>

This is the part of a navgraph

<fragment
    android:id="@+id/productDetailsFragment"
    android:name="com.life4.ecommerce.view.ProductDetailsFragment"
    android:label="fragment_product_details"
    tools:layout="@layout/fragment_product_details">
    <argument
        android:name="productID"
        app:argType="integer" />
    <action
        android:id="@+id/action_productDetailsFragment_to_profileFragment"
        app:destination="@id/profileFragment" />
    <action
        android:id="@+id/action_productDetailsFragment_to_imageDetailsFragment"
        app:destination="@id/imageDetailsFragment" />
    <deepLink
        android:id="@+id/deepLink2"
        app:uri="example.com/pro/{productID}" />
</fragment>
1 Answers

It might happen if your nav graph contains sum included graph inside. Opening with deep link intent data must have flat graph that your app can navigate through your fragment. One the other solution to your problem is using extra instead of data for example when you want to start your activity with intent that can active deeplink of fragment, you can use extra and action view. See below code for better understanding the solution. First this is hard solution that have to exist flat nav graph

val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.data=Uri.parse("your deeplink url")

And you can use below code that is better and your nav graph can not change

val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.putExtra(INTENT_KEY,"your deeplink url")

and you can handle your intent data in your activity like this

intent?.getStringExtra(INTENT_KEY)?.let {
      navController.navigate(Uri.parse(it))
}
Related