Android Q: Sharing shortcuts

Husayn Hakeem
ProAndroidDev
Published in
5 min readMar 17, 2019

--

Android Q introduces sharing shortcuts, a faster way to share content from your app, think of it as direct share on steroids. In this article you’ll learn about the difference between the two, and how to implement sharing shortcuts.

Direct share vs sharing shortcut

The direct share API introduced in android M has been replaced with the sharing shortcut API. On Android Q -and higher-, the direct share mechanism will continue to work, but the share sheet will put higher priority on share targets provided using the new API.

Sharing shortcut (left) vs Direct share (right). Not much of a difference, huh

Another important difference between the two APIs is that the new one uses a push model whereas the old one uses a pull model. Push as in the app provides (hence, pushes) a set of share targets ahead of time (when the app first launches for example), making the process of retrieving share targets faster when preparing the share sheet. This is in contrast with the Pull model, where specific custom chooser services are queried by the system while preparing the share sheet.

One last important difference is in the implementation. Using direct share in your app requires implementing a custom ChooserTargetService, whereas sharing shortcut doesn’t, since the compatibility library already defines it for you.

On a side note, what’s a chooser service again?

When a user, for example, wants to share text content from an app, the share sheet is displayed. While preparing the sheet, the system queries certain chooser services in order to retrieve a list of chooser targets, which can be a frequently emailed contact on your device, a recently active group messaging conversation or any other relevant item.

Technically speaking, the chooser service is an Android service that’s called by the system when a user asks explicitly to choose a target for an intent by another app, and returns a list of chooser targets which deep link into the selected app.

Which chooser services are queried depends on the system, the decision is made based on certain criteria such as the frequency and recency of usage of the app, and how much time was spent on the app.

Implementing sharing shortcuts

Firstly, declare share target elements in a xml resource file (res/xml/shortcuts.xml). The xml code looks something like this.

  • <data> represents the type of the shared content, it’s similar to the data specification you’re used to seeing in <intent-filter>s.
  • <category> is used to match the app’s published shortcuts with its share target definition. Its value can be arbitrary and is up to you to define. Note that a single <share-target> element can contain multiple categories.
  • <share-target> defines a shortcut in the app. In the example above, it deep links into SendMessageActivity. which in consequence must be able to respond to incoming intents with an action SEND and a content type text/plain. Its declaration inside your app’s manifest should look something like this.

Secondly, Add a <meta-data> element to an activity in your manifest. The documentation states that it should be inside the launcher activity, but I put it inside another activity declaration and it worked just fine. To be safe, stick to the official documentation.

Finally, publish -and manage- your sharing shortcuts! You can do this inside your launcher activity for example. Below is an example of creating a sharing shortcut and associating it with your app.

  • setPerson()/setPersons() associates one or more Person objects to a shortcut. Adding them to a shortcut is optional, but strongly recommended if the share target can be associated with a person. For example in a messaging app, setPerson() can be used to associate a contact with a share target, and setPersons() can associate a share target with a group chat.
  • ShortcutManagerCompat adds backwards compatibility with the old DirectShare API, and is now the preferred way to publish share targets.

One last -awesome- thing!

Any shortcuts published for a share target purpose are also launcher shortcuts, and will be shown in the menu when long pressing the app’s icon.

Sharing shortcut demo app

I built a sample app that demonstrates sharing shortcuts. It allows sharing text messages with some colorful androids! Check it out to see how to use sharing shortcuts in more detail.

Attempting to share text from the chrome browser opens up a share sheet, with 2 sharing shortcuts (one to “Orange droid”, another to “Purple droid”). Clicking on the “Orange droid” opens up a screen from the demo app with the recipient and message information loaded!

Clicking on the “Orange droid” shortcut opens a pre-populated activity ready to share

Following the same scenario, but this time clicking on the app icon for the demo app in the list of apps in the share sheet triggers another flow. Since a recipient wasn’t chosen, an activity prompting the selection of a droid is first displayed before redirecting to the same screen allowing the user to share their message.

Clicking on the app’s icon opens a droid (recipient) selection screen before continuing with the flow from the previous example

For more on Java, Kotlin and Android, follow me to get notified when I write new posts, or let’s connect on Github and Twitter!

--

--