Exploring Android 12: Recents URL Sharing (Pixel only)

M Farhan Majid
4 min readMar 17, 2022

--

Android 12 introduces a rather unique new feature for Google Pixel device users. This new feature is Recents URL Sharing. As you can see in the image below, Recents URL Sharing enables user to share a URL from the Recents screen. The URL shared usually corresponds to the content that is currently displayed on the app. For example, for a browser app, the URL will be the same as the webpage that you’re currently viewing.

With Recents URL Sharing, you can share URL from the Recents screen. This feature is only available in Google Pixel devices. (Source: developer.android.com)

In this article, we will create a simple application that uses this new feature. Let’s go!

Where’s the Source Code?

Step-by-Step Explanation

Follow the steps provided below to make this application.

1. Open Android Studio. Create new project with “Empty Activity” option.

2. We are going to make an application with two pages/screens. Each page will correspond to a different URL. So first, we are going to add Navigation component to our app/build.gradle file like this:

3. Next, create the first page by creating a new Fragment by clicking menu File > New > Fragment > Fragment (Blank). Fill the Fragment Name field with “FirstFragment” and the Fragment Layout Name field with “fragment_first”. Click Finish.

4. Repeat the previous step for creating the second page. This time, fill the Fragment Name field with “SecondFragment” and the Fragment Layout Name field with “fragment_second”.

5. Next, let’s prepare the layouts. First we are going to create the first page layout. The image shown below shows how the first page will look like. It displays a button to navigate to the second page and a text displaying the current URL.

This is how the first page should look like (fragment_first.xml)

To create this layout, open fragment_first.xml file and copy-paste the code below:

6. Next we will create the second page layout. The image below shows what the second page looks like. It will only contain a text displaying the current URL.

This is how the second page should look like (fragment_second.xml)

To create this layout, open fragment_second.xml file and copy-paste the code below:

7. Next we need to create a Navigation graph for our app by right-clicking the res directory in the Project window and select New > Android Resource File. Type “nav_graph” in the File name field and select “Navigation” in the Resource type field. Click OK.

8. In this app, we want to create a simple navigation graph with the FirstFragment as the initial page and an action that navigates user from FirstFragment to SecondFragment. You can see in the image below how this navigation graph will look like in the Navigation Editor.

Our app’s navigation in Android Studio’s Navigation Editor.

You can create this navigation by yourself or you can just open the nav_graph.xml file and copy-paste the code below:

9. Next, we will update our activity_main.xml file. You can go ahead and copy-paste the code snippet below. The file will only contains FragmentContainerView for containing the fragments.

10. We also need to update our FirstFragment.kt file. As you can see in the code snippet below, the most important thing to notice is how it setup the navigation to the second page.

11. And we also need to update our SecondFragment.kt like shown below:

12. Lastly, we will update our MainActivity.kt file. This is the last and most important step in this article because this file is where the Recents URL Sharing feature will be implemented. You can see what it looks like in the code snippet below, but here’s the gist of it. You need to override the onProvideAssistContent method in your Activity to implement Recents URL Sharing. Inside the method, you need to call setWebUri (can be shorten to webUri in Kotlin) to set the URL that you want to share. In this example, the shared URL will be changed every time the destination (page) change.

13. That’s it! Now run this app on a Pixel device and you should see Recents URL Sharing in action like this:

Recents URL Sharing in action! Notice how the URL changes when you go to different page.

As always, thanks for reading!

Want to learn more about Android 12?

Check out all of our articles from “Exploring Android 12” series here:

Exploring Android 12

9 stories

--

--