With the version of Android OS 7.1 (API 25), Google offers us a new mechanism for working with applications -
Android Shortcuts . We wrote a brief overview of this feature, created for the convenience of users.

general review
Translated from English "shortcut" is the shortest path, this name well reflects the main purpose of shortcuts. They allow us to get to any particular point of the application and perform a specific action in just a couple of clicks. Shortcuts look good, for example, if the user needs:
• select a predetermined route in the navigator;
• start a conversation with a friend in the messenger;
• continue the game from the last save point.
')

Main components
Shortcut includes:
1) an intent or an array of intents that trigger a particular action;
2) an identifier or ID defining this particular shortcut;
3) short and long messages displayed when it is displayed;
4) icon to the left of the text.
To better understand the mechanics of working with shortcuts, consider their classification.
Types of shortcuts
Android offers 2 types of shortcuts (in fact, there are three of them, but more on that below): static and dynamic. Next, we consider each type of shortcuts with small examples and explanations.
Static shortcuts
The description of static shortcuts is contained in the
xml
file. They are created once, they cannot be deleted, added and changed without updating the application, but they are simple enough to create.
Creation exampleThe
shortcuts.xml
file contained in
res/xml/
:
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android"> <shortcut android:shortcutId="id1" android:enabled="true" android:icon="@drawable/shortcut_search" android:shortcutShortLabel="@string/shortcut_short_label" android:shortcutLongLabel="@string/shortcut_long_label" android:shortcutDisabledMessage="@string/disabled_message"> <intent android:action="android.intent.action.VIEW" android:targetPackage="com.example.myapplication" android:targetClass="com.example.myapplication.MainActivity" /> <categories android:name="android.shortcut.conversation" /> </shortcut> </shortcuts>
And a couple of lines in
AndroidManifest.xml
:
<activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" /> </activity>
Delete shortcuts or change its components will not work until the application is updated.
Static shortcuts are recommended for those actions that do not exactly change due to user manipulations with the application and will always be available. For example, open a new search query, go to the notification settings, start a new conversation. However, it is not a good idea to use a static shortcut, for example, in order to send the user immediately to the authorization screen. And what if he is already authorized? Then this action does not make sense. In such cases, we will need dynamic shortcuts.
Dynamic shortcuts
They can be added, deleted, modified in real time, that is, depending on certain events. Let's return to the example with authorization: if the user is not authorized, then we add this shortcut, as soon as it is authorized, we delete it.
All interaction with shortcuts occurs programmatically via the
ShortcutManager class, which is available for version 23 of the API, but most of its methods are only available for version 25 of the API. Operations with them are quite simple, but there are some nuances that will be discussed in detail later.
Creation example ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1") .setShortLabel("Search") .setLongLabel("Start new search") .setIcon(Icon.createWithResource(context, R.drawable.shortcut_search)) .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("shortcutapp://search"))) .build(); shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut));
Removing and modifying dynamic shortcuts is also quite simple:
Deletion ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); shortcutManager.removeDynamicShortcuts(Arrays.asList("id1", "id2"));
Change ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); shortcutManager.updateShortcuts(Arrays.asList(shortcut1, shortcut2));
Pinned shortcuts
In fact, there is another type of shortcuts - this, literally translated, “pinned” or “pinned” shortcuts. They look like this:
Creating such shortcuts programmatically can not, as well as delete. This can only be done by the user. But with the help of the code they can be disabled (
disableShortcuts()
). You can fix both static and dynamic shortcuts.
Static caseIn the case of a static shortcut, only the OS itself can zadizayblit appropriate fixed, but only if after updating this static shortcut is missing. If you try to manually zadizeyblit it, then IllegalArgumentException
will fall out with the message that static shortcuts can not be changed dynamically.
Dynamic caseIf you delete a dynamic shortcut, then the corresponding shortcut will be removed (“fixed”), and it can be used. On the one hand, this is a serious advantage that allows you to expand the number of shortcuts, but on the other hand, it is a potential source of bugs, since the removal of a dynamic shortcut can be associated with the removal of the appropriate functionality for processing an action.
Before deleting a dynamic shortcut, make sure that the pinned one will also be disabled, otherwise it may cause problems in the application.
With any shortcut, you can set the appropriate message that will be displayed to the user when you click on it.
Example ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); shortcutManager.disableShortcuts(Collections.singletonList(id), "Removed");
We familiarized with shortcuts and the basic principles of their work as a whole. Now you can go to the details, features and practical advice.
Details and details of the application
1. When adding shortcuts in most cases they will be displayed from the first added to the last, the first added will be closest to the application icon. First static will be displayed, then dynamic.
Shortcuts RanksAndroid Development claims that every shortcut has a rank - you can get it using getRank()
. But you can't set this rank yourself, the OS sets it for its own reasons. While in all cases that we met, this rank was set depending on the order of addition. Rank, by the way, is a non-negative number, and the lower this number, the “priority” is considered shortcut, the closer it will be to the application icon (rank 0 is the closest).
2. When creating a shortcut, you need to set a long and short message. A long message is indicated in the list; if it does not fit, then a short one. Google recommends setting a short message to a maximum of 10 characters, and a long one to a maximum of 25.
Supplementary FactsIf both messages are too large, then a short abbreviated form will be displayed. Also, “fixed” shortcuts are in any case signed with a short message, even if there are more characters in a short message than in a long one.
3. Although you can add as many as five shortcuts, Google recommends that you do not add more than four. And more than four shortcuts will not appear in the list (the last one added will not appear). Why we need the fifth shortcut - a mystery, because it is not displayed on the tablets either.
4. The static shortcuts targetPackage attribute has one peculiarity - it can only have a hardcode value. If you have different applicationId suffixes for different application assemblies, then when declaring a
@string/shortcut_target_package
, you will not be able to specify the value of the targetPackage link, for example,
@string/shortcut_target_package
. You will have to create a
shortcuts.xml
file separately for each
buildFlavour
application and prescribe the necessary
targetPackage
manually with the necessary suffix
applicationId
.
5. It is important that Android itself does not regulate the number of added shortcuts. If for some reason you want to add a sixth shortcut, the application will simply fall, throwing
IllegalArgumentException: Max number of dynamic shortcuts exceeded
. Independently monitor the number of added shortcuts.
6. If the action attribute is not set to at least one shortcut intent, then in the case of a dynamic shortcut, the application will drop when creating this shortcut, and in the case of static shortcuts, the shortcut will simply not be created. Note that you cannot put some types in extras of intents of the shortcut, for example,
Serializable
, the valid types can be seen in the
PersistableBundle sources.
7. If you try to call
getSystemService(ShortcutManager.class)
on a device below 23 API, then the application will drop with a
ClassNotFoundException
.
8. When adding several intents to the shortcut, which are processed by different activities, you can build a certain stack of activities. The bottom of this stack will be the activity that processed the first added intent.
Implementation in practice 9. If the user is blocked on the device, then he will not be able to use shortcuts - they are contained in an encrypted storage.
10. If the user wants to support the application on another device, then only the “fixed” shortcuts will be recreated on it. Dynamic and static are not recreated, but static will be re-defined when the application is installed. If you are worried about application support on different devices, and you have not set the
allowBackup = false
flag in the Manifest, you only need to take care of manually recreating dynamic shortcuts.
11. Android developers also suggest that we use the
reportShortcutUsed(String id)
method when executing an action from the corresponding shortcut — both when clicking on the shortcut and locally in the application, if the action is the same. According to the developers, there should be statistics on the use of certain actions from shortcuts, and based on this statistics, the user can be offered certain shortcuts. Perhaps this is where the potential of the fifth shortcut should unfold.
Principle of operationSuppose, for example, that the user chooses the way home in the navigator at 7 o'clock every night and this action is stored in the shortcut. Based on this, this shortcut will be a priority when it is shown next time at 7 pm However, in practice, I have not yet met a single case of the implementation of this feature, so I can not confirm or deny this information.
Finally, a couple of not yet mentioned tips from Android developers.
1. Update shortcuts (
updateShortcuts()
) if their content and value has not changed, but only the displayed message or icon has changed. If you want to remove one shortcut and add another, then do not use the update, perform the delete operation, and then add. Ignoring this advice, you can get all sorts of incidents with "fixed" shortcuts.
2. Follow
the shortcut design guidelines . This is a small document that is easy to follow.
Main• the icon should be round in the general case (diameter - 44 dp);
• inside the icon there should be a simple icon, like a plus sign (size - 24 x 24 dp);
• Colors must be consistent with the general theme of the application
• when choosing an icon, remember that it will be displayed on the “fixed” shortcuts along with the main application icon at the bottom right, so you should not choose something like this:
Conclusion
Android Shortcuts is a powerful tool for working with applications. Serious multi-functional applications should use it to keep up with the times. I hope this guide will inspire you to add such a mechanism to your applications and help you embed it seamlessly. You should not use shortcuts in your application, if you know that they are needed there just like a fifth wheel cart. Remember the relevance, because the shortcuts are not added to extend the functionality, but for the convenience of the user.