Manage shortcuts

After creating shortcuts, you may need to manage them over the lifetime of your app.
For example, you may want to optimize your app by determining how often your users complete
specific actions with your shortcuts. In another case, you might decide to disable a pinned
shortcut to prevent your app from performing outdated or missing actions. This guide describes
these and several other common ways to manage your shortcuts.

Shortcut behavior

The following sections contain general information about shortcut behavior including
visibility, display order, and ranks.

Shortcut visibility

Important security note: All shortcut information is stored in
credential encrypted storage,
so your app cannot access a user's shortcuts until after they've unlocked the device.

Static shortcuts and dynamic shortcuts appear in a supported launcher when the user performs a
specific gesture. On currently-supported launchers, the gesture is a long-press on the app's
launcher icon, but the actual gesture may be different on other launcher apps.

Note: Ranks are auto-adjusted so they're unique for each type of shortcut
(static or dynamic). For example, if there are three dynamic shortcuts with ranks 0, 1 and 2,
adding another dynamic shortcut with a rank of 1 represents a request to place this shortcut in
the second position. In response, the third and fourth shortcuts move closer to the bottom of the
shortcut list, with their ranks changing to 2 and 3, respectively.

Manage multiple intents and activities

If you want your app to perform multiple operations when your user activates a shortcut, you can
configure it to trigger successive activities. You can accomplish this by assigning multiple
intents, starting one activity from another, or setting intent flags, depending on the shortcut's
type.

Assign multiple intents

When creating a shortcut with ShortcutInfo.Builder,
you can use
setIntents() instead of
setIntent(). By calling setIntents(), you can launch multiple
activities within your app when the user selects a shortcut, placing all but the
last activity in the list on the
back stack. If the user then decides to
press the device's back button, they will see another activity in your app instead of returning
to the device's launcher.

Note: When the user selects a shortcut then presses the back key, your
app launches the activity corresponding with the shortcut's second-to-last intent listed in the
resource file. This behavior pattern continues upon repeated presses of the back button, until the
user clears the back stack that a shortcut created. When the user next presses the back button,
the system navigates them back to the launcher.

Set intent flags

Dynamic shortcuts can be published with any set of
Intent flags.
Preferably, you'll specify
Intent.FLAG_ACTIVITY_CLEAR_TASK along with your other flags.
Otherwise, if you attempt to start another task while your app is running, the
target activity might not appear.

Update shortcuts

Each app's launcher icon can contain at most
getMaxShortcutCountPerActivity() number of static and dynamic shortcuts combined.
There is no limit to the number of pinned shortcuts that an app can create, though.

When a dynamic shortcut is pinned, even when the publisher removes it as a dynamic shortcut,
the pinned shortcut is still visible and launchable. This allows an app to have more than
getMaxShortcutCountPerActivity() number of shortcuts.

Later, the user has started three additional conversations (c5, c6, and c7), so
the publisher app re-publishes its dynamic shortcuts. The new dynamic shortcut list
is: c4, c5, c6, c7.

The app has to remove c1, c2, and c3 because it can't display more than four dynamic shortcuts.
However, c1, c2, and c3 are still pinned shortcuts that the user can access and launch.

The user can now access a total of
seven shortcuts that link to activities in the publisher app. This is because the total
includes both the maximum number of shortcuts and the three pinned shortcuts.

The app can use
updateShortcuts(List) to update any of the existing seven shortcuts.
For example, you might update this set of shortcuts when the chat peers' icons have changed.

The
addDynamicShortcuts(List) and
setDynamicShortcuts(List) methods can also be used to update existing shortcuts with
the same IDs. However, they cannot be used for
updating non-dynamic, pinned shortcuts because these
two methods try to convert the given lists of shortcuts to dynamic shortcuts.

To learn more about our guidelines for app shortcuts, including updating shortcuts, read
Best practices.

Handle system locale changes

Apps should update dynamic and pinned shortcuts when they receive the
Intent.ACTION_LOCALE_CHANGED broadcast, indicating that the system locale has changed.

Track shortcut usage

To determine the situations during which static and dynamic shortcuts should appear, the launcher
examines the activation history of shortcuts. You can keep track of when users complete specific
actions within your app by calling the
reportShortcutUsed() method and passing it the ID of a shortcut, when
either of the following events occur:

The user selects the shortcut with the given ID.

Within the app, the user manually completes the action corresponding to the same shortcut.

Disable shortcuts

Because your app and its users can pin shortcuts to the device's launcher, it's possible that
these pinned shortcuts could direct users to actions within your app that are out of date or
no longer exist. To manage this situation, you can disable the shortcuts that you don't
want users to select by calling
disableShortcuts(), which removes the specified shortcuts from the static and dynamic
shortcuts list and disables any pinned copies of these shortcuts. You can also use an
overloaded version of this method, which accepts a
CharSequence as a custom error
message. That error message then appears when users attempt to launch any disabled shortcut.

Note: If you remove some of your app's static shortcuts when you update your
app, the system disables these shortcuts automatically.

Rate Limiting

When using the
setDynamicShortcuts(),
addDynamicShortcuts(), or
updateShortcuts() methods, keep in mind that you might only be able to call these
methods a specific number of times in a background app, an app with no activities or
services currently in the foreground. The limit on the specific number of times you can call
these methods is called rate limiting. This feature is used to prevent
ShortcutManager
from over-consuming device resources.

When rate limiting is active,
isRateLimitingActive() returns true. However, rate limiting is reset during certain
events, so even background apps can call ShortcutManager
methods until the rate limit is reached again. These events include the following:

If you encounter rate limiting during development or testing, you can select
Developer Options > Reset ShortcutManager rate-limiting from the
device's settings, or you can enter the following
command in adb:

$ adb shell cmd shortcut reset-throttling [ --user your-user-id ]

Backup and restore

You can allow users to perform backup and restore operations on your app when changing devices
by including the
android:allowBackup="true" attribute assignment in your app's manifest file.
If you allow backup and restore, keep the following points about app shortcuts in mind:

Static shortcuts are re-published automatically, but only after the user
re-installs your app on a new device.

Dynamic shortcuts aren't backed up, so you must include logic in your
app to re-publish them when a user opens your app on a new device.

Pinned shortcuts are restored to the device's launcher automatically, but the system
doesn't back up icons associated with pinned shortcuts. Therefore, you should save your
pinned shortcuts' images in your app so that it's easy to restore them on a new device.

The following code snippet shows how best to restore your app's dynamic shortcuts and
how to check whether your app's pinned shortcuts were preserved: