Home » Navigation component deep link example

Navigation component deep link example

  • by
Android Navigation component deep link example

1. Overview

In this article, we will learn an example for the Android navigation component deep link. If you also want a high-level overview of the concepts, refer to the Android Navigation component article.

For using a single activity with multiple fragments, refer Android navigation component example.

2. Deep link example

An Android deep link takes you directly to a specific destination (activity or fragment) within the app. The Navigation component provides two different deep links: 

  1. explicit 
  2. implicit

3. Navigation component implicit deep link

Assume you want to open your application and navigate to a specific destination (fragment or activity) when a user clicks on a link.

For example, we have an Android app that should open and navigate to SecondFragment whenever a user clicks on the link tedblob.com/android/ from their device.

1. In the first place, open your navigation graph and add the <deeplink/> element to the destination SecondFragment. You can specify URI, ACTION, and MIME Type.

<fragment
    android:id="@+id/secondFragment"
    android:name="com.tedblob.kotlin.navigationcomponentexample.SecondFragment"
    android:label="fragment_second"
    tools:layout="@layout/fragment_second" >
    <deepLink
        android:id="@+id/deepLink"
        android:autoVerify="true"
        app:action="ACTION_VIEW"
        app:mimeType="text/html"
        app:uri="tedblob.com/android/" />
</fragment>

2. Now, also associate your activity that contains the SecondFragment with its corresponding navigation graph in the manifest file:

<activity
        android:name=".MainActivity"
        android:exported="true">
        <nav-graph android:value="@navigation/nav_type" />
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
       </intent-filter>
</activity>

3. Now, run your application and open the link tedblob.com/android/ such as notepad or SMS.

Android navigation component deep link
Deep link in SMS
Android navigation component implicit deep link
Dialog to select app

If the application is already running, the SecondFragment launches a new task. If you want to have a single task, then specify the launch mode in the manifest file and handle deep link in your activity’s onNewIntent function:

<activity
            android:name=".MainActivity"
            android:launchMode="singleTask"
            android:exported="true">
            <nav-graph android:value="@navigation/nav_type" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
override fun onNewIntent(intent: Intent?) {
   super.onNewIntent(intent)
   Navigation.findNavController(this, R.id.nav_host_fragment).handleDeepLink(intent)
}

4. Android navigation component explicit deep link

Assume you want to create a notification with a deep link to your application’s specific page. So, you had to use the PendingIntent.

An explicit deep link of the Navigation component uses the PendingIntent to take users to a specific location within your app.

In the below code, the Navigation component provides an explicit deep link that uses the PendingIntent. You can pass this intent to the Notification builder.

val pendingIntent = NavDeepLinkBuilder(context)
    .setGraph(R.navigation.nav_type)
    .setDestination(R.id.secondFragment)
    .setArguments(args)
    .createPendingIntent()

val builder = NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("Navigation component")
        .setContentText("Explicit deep link!")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setContentIntent(pendingIntent)
        .setAutoCancel(true)

You can also use NavController to create deep link.

val pendingIntent = navController
    .createDeepLink()
    .setGraph(R.navigation.nav_type)
    .setDestination(R.id.secondFragment)
    .setArguments(args)
    .createPendingIntent()

val builder = NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("Navigation component")
        .setContentText("Explicit deep link!")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setContentIntent(pendingIntent)
        .setAutoCancel(true)

When the user clicks on the notification, Android clears the back stack of the task and loads the SecondFragment destination on the screen.

However, when the user presses the back button from the SecondFragment destination, they navigate back up the navigation stack as if they entered the app from its entry point.

5. Conclusion

To sum up, we have seen examples for the Android deep link.

Leave a Reply

Your email address will not be published. Required fields are marked *