[MUSIC PLAYING]
CHET HAASE: Welcome to this fourth episode
in the Navigation series.
Today I'm going to talk about deep links.
Sometimes you want to make it easy for the user
to navigate down into a deeper part of your hierarchy
and your application without having
to launch the application, then click, click,
click through that hierarchy.
For example, maybe you want to make it easy
for them to return to an ongoing conversation in a messaging
app.
Or you want to make it easy for them to immediately get
to a shopping cart form in the middle of a shopping
application.
You can use deep links to do this,
surfacing those links outside of the application in ways
like using notifications or shortcuts where
they can click on that link and immediately takes them
to where they want to go inside of your app.
In the Donut Tracking app, which we've
seen in previous episodes, there are
a couple of ways that I would like
to surface this capability.
For example, if I'm having a fantastic donut experience
in the wild, I'd like to add new information about that donut.
I'd like to have a shortcut so that I can immediately
get to that Add Donut screen.
Or sometimes I've added information and edited
information about existing donuts in the application,
and I'd like to return to that to continue
editing information for the one that I was just editing.
I can use two different kinds of deep links for that.
I can use an implicit deep link, which
always goes to the same static place in the application.
Or I can use an explicit deep link,
which uses a PendingIntent to get to dynamic content
within the application.
The Navigation component makes both of these types of links
easier with UI built into the design
tool as well as APIs built into the library itself.
We're going to use the Donut Tracking
application to show how to create both
of these types of links today.
Let's start with the implicit deep link
to add a new donut to the system.
First of all, we need to create the deep link.
You can do this in the Navigation
Editor, which is where a lot of the navigation stuff happens.
Click on the Dialog destination.
This is the thing that we saw in previous episodes
where we enter information about either
a new donut or an existing donut in the database.
This shows the properties over on the right.
And you can see I've already created the deep link.
Isn't that handy?
If we double-click on this, I have entered information
in here, which is a URI, Universal Resource Identifier.
Note that I have a custom URI in here,
because I don't want this to get misinterpreted
as some web link that brings up a browser on the device.
I just want it to go into my application.
I've entered this information here.
And now I need to enter it elsewhere
in the system to make sure that the
build knows how to handle this.
But note that this is a deep link that
takes us to this destination.
Also note that when it gets to this destination,
if it does not have an arg associated with it.
We will provide a default arg of negative 1,
which is a trigger inside the system,
as we saw in a previous episode, to say this is a new donut.
Please enter information about the new donut
into the database.
From here, let's go take a look at the manifest.
And we can see I've entered a little bit
of special information here, some metadata saying, hey, I
have some shortcuts to find.
Check out the following file.
If we take a look at that Shortcuts file,
we can see here is a shortcut.
I've got an ID.
I've got some information, some text in there.
All of the information about what
to actually do with that shortcut
is embedded in that URI.
And the Navigation [INAUDIBLE] knows the right thing
to do to take us to the appropriate destination
within the application.
But this is the important part.
You'll note that the data here, inside the intent,
uses the same URI that I use in entering the deep link
into the navigation graph.
Instead of running the application,
we can just long click on the application
that we've installed.
And we can see our shortcut right there with a label
that we created in that Shortcuts Metadata file.
And then if we click on it, it will take us
to that deep link within the application.
And now we can enter new information
about a new, exciting donut.
That was implicit deep links.
Now let's create an explicit deep link, which
will be created dynamically based
on the state of the app at runtime.
If you're like me, then donuts are really important to you.
When I'm entering information about a new donut find,
I probably want to take my time with it.
Maybe I'll start the entry with some information,
but I may want to come back later
and enter more when I've had a really good chance
to appreciate the experience deeply.
I can do this with notifications.
When I enter information about a donut,
the app will create a notification
that makes it easier to get back to that ongoing entry.
There's not a lot of code that we have to write here.
We basically just need to create a PendingIntent
to get to the right place in our application
and create a notification with that PendingIntent.
Most of this happens in DonutEntryDialogFragment.
Let's go to that now.
We've seen this code before.
This is the code that kicks in when the user clicks the Done
button, right?
They have finished editing or entering information
about a new donut.
They click Done.
And then we save it in the database.
But here's the new code.
After we enter this information, then we
create a notification that goes into the system
so that we can get back to that information
that they were editing before.
This code is called with the donut ID from the database,
so that we know we use this as a trigger to get back
to the correct data.
And we go ahead and create an arg from that.
This is going to be the bundle that
gets passed in with navigating to the destination
of this dialog.
And then we create a PendingIntent.
And this is an API.
This is part of what Navigation component provides you,
is a deep-link API.
On the Navigation Controller, you call this createDeepLink
function.
And you set the destination to this dialog fragment
where we're going to continue editing the information.
You add the arg to it.
You create the PendingIntent, and you're all set to go.
Now all you need to do is post the notification.
I created this utility class called Notifier.
And postNotification will create and then post
the notification for us.
First of all, we create a builder
note that we have to call it with a channel ID.
And we do this--
we create this channel ID up here in the init function.
So we already know that the channel ID
exists by the time we get here.
Then we set some information in there
about what text is going to be in that notification.
And then we actually create the notification.
We set the text.
We set the priority.
We set the intent, which is the intent that we passed in.
And we go ahead and build in the notification.
And then we cancel notifications if they exist already.
I only want to get back to the latest donut editing
process that I'm involved in.
I don't want to continue editing all of the donuts
that I happened to enter all on that day.
That would be far too many in the notification panel.
We'll go ahead and cancel any existing notifications
before we then go ahead and create the new notification.
Then that will post on the system,
and we'll be able to click on that notification, get into it.
If we run the application, we can see that happen.
There is our application.
Let's go ahead and create a new donut here.
We'll call it fundonut.
And we'll give it a rating of four
because it was pretty good.
And now you can see that a notification appeared.
And we get this ability to click on the deep link.
But just to show it really in action,
I'm going to go out to the home screen.
And I can click on the deep link,
and it immediately takes us to that dialog
where we continue editing more information about our donut.
In this episode, we learned how to create
an implicit deep link, which takes us
to a form that allows us to enter
new information about a new donut into our system,
as well as explicit deep links, which allow us to get back
to an editing operation where we were already entering
information and we'd like to continue that
from a notification.
This Donut Tracking app is getting better and better.
It is not as good as a donut, but then, nothing is.
If you like the video, go ahead and like, and share,
and subscribe to the Android Developers channel on YouTube.
Thanks.
[MUSIC PLAYING]