Notes: 12. Deep Link with Custom URI
Explore more resources on our website related to Navigation, such as our The Navigation Architecture Component Tutorial: Getting Started, Navigation Component for Android Part 2: Graphs and Deep Links and Navigation Component for Android Part 3: Transition and Navigation. Also stay tuned for more materials coming soon! :]
In production apps, Navigation graph is likely to be complex and there could be a requirement to navigate directly to some intermediate screen from one of initial screens.
You can utilize Deep link with custom URI for this purpose. It allows you to navigate to any fragment by URI.
In this episode, you will create a deep link to Welcome screen from Main screen.
Open the starter project for this episode. Go to nav_graph.xml. In the Design view, with WelcomeFragment selected, find the Deep Links section on the Attributes panel.
Click + sign, add the following in the URI textfield:
authorizeme://home/welcome
Click Add button. Go to code view, confirm deeplink added for WelcomeFragment. Here, you set WelcomeFragment as destination for this particular URI.
Now, go to MainFragment. Inside OnClickListener of fab, add the following code:
v ->
v.findNavController().navigate(Uri.parse("authorizeme://home/welcome"))
Import android.net.Uri for Uri. Here, on the click of fab, you navigate via URI that you have just configured.
Build and run the app to see the Navigation in action!
Click on fab and you lend at Welcome screen.Congratulations! Custom URI based deep links is just a single line of code! Cool, isn’t it :]