Jetpack Navigation: Getting Started

Aug 10 2021 · Kotlin 1.4, Android 11, Android Studio 4.2.1

Part 3: Add Animations & Deep Links

10. Implement an Explicit Deep Link

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 09. Global Actions Next episode: 11. Associate a Web Link with a Destination

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 10. Implement an Explicit Deep Link

Android Apps don’t have just single entry point from Launcher Icon. Other entry points are Notifications, App widgets , Web URLs etc.

All take user to a specific screen or content in the App - which is called Deep linking. Navigation Architecture component supports both Explicit and Implicit Deep links.

Explicit side are around Notifcations, App Shortcuts, App widgets, new Actions and slices.These are things you create and are usually pending Intent based. These are the things that you pass to another app or System to say that I want to go to this specific content in my app.

The implicit side are around Web URLs and Custom Scheme URIs. So these would be other app launching your app.

Navigation component uses a class called NavDeepLinkBuilder whose job is to deep link to specific destination in the navigation graph by its ID.

In this episode, you’ll implement an Explicit Deep link using App Widget. A widget is a small gadget or control of your android application placed on the home screen

Starter project alreay includes all files necessary to create an App Widget. Let’s have a quick tour of it.

Have a look at res > layout > authorizeme_appwidget.xml. This layout is displayed as a widget on user’s home-screen. Next look at res > xml > authorizeme_appwidget_info.xml.

In the, initialLayout, provide reference of widget layout file.

The widget’s update method is called when the specified time is reached in a millisecond.

Next, look at AuthorizeMeWidgetProvider. AuthorizeMeWidgetProvider extends AppWidgetProvider & AppWidgetProvider extends BroadcastReceiver. So, AuthorizeMeWidgetProvider is indirectly a child of BroadcastReceiver and need to be declared in AndoidManifest.xml

pendingIntentspecifies whst happens when user clicks on Widget from Home screen. At present, it opens up Google.com Website. Last part of widget, look at AndroidManifest.xml.

Here, you include the required entry for AuthorizeMeWidgetProvider. Open AuthorizeMeWidgetProvider. Here, existing Pending Intent specifies it takes user to google.com on clicking the Widget.

You’ll modify this to Deep link into WelcomeFragment instead. Comment out existing Pending Intent:

/*    val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"))
    val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0)*/

and add following code:

   val args = Bundle()
        args.putString("param1", "Test User")
        args.putString("param2", "Handled Link From Widget")
        val pendingIntent = NavDeepLinkBuilder(context)
                .setGraph(R.navigation.nav_graph)
                .setDestination(R.id.welcomeFragment)
                .setArguments(args)
                .createPendingIntent()

Import android.os.bundle and androidx.navigation.NavDeepLinkBuilder .

setGraph includes the navigation graph. setDestination specifies where the link goes to. setArguments includes any arguments you want to pass into your deep link.

Build and run the app to see the Explicit Deep link using App widget in Action.

Click back to return to Home screen. Long press on Home screen, click on Widgets, now click on Widget that appears for AuthorizeMe. Long press on it , and it gets added to Home screen.

Click on Widget, it deep links to WelcomeFragment with parameters specified. Congratulations!