Android Dev Summit, October 23-24: two days of technical content, directly from the Android team. Sign-up for livestream updates.

ShortcutManagerCompat

public class ShortcutManagerCompat
extends Object

java.lang.Object
   ↳ androidx.core.content.pm.ShortcutManagerCompat


Helper for accessing features in ShortcutManager.

Summary

Constants

String EXTRA_SHORTCUT_ID

Key to get the shortcut ID from extras of a share intent.

Public methods

static boolean addDynamicShortcuts(Context context, List<ShortcutInfoCompat> shortcutInfoList)

Publish the list of dynamic shortcuts.

static Intent createShortcutResultIntent(Context context, ShortcutInfoCompat shortcut)

Returns an Intent which can be used by the launcher to pin shortcut.

static List<ShortcutInfoCompat> getDynamicShortcuts(Context context)

Return all dynamic shortcuts from the caller app.

static int getMaxShortcutCountPerActivity(Context context)
static boolean isRequestPinShortcutSupported(Context context)
static void removeAllDynamicShortcuts(Context context)

Delete all dynamic shortcuts from the caller app.

static void removeDynamicShortcuts(Context context, List<String> shortcutIds)

Delete dynamic shortcuts by ID.

static boolean requestPinShortcut(Context context, ShortcutInfoCompat shortcut, IntentSender callback)

Request to create a pinned shortcut.

static boolean updateShortcuts(Context context, List<ShortcutInfoCompat> shortcutInfoList)

Update all existing shortcuts with the same IDs.

Inherited methods

Constants

EXTRA_SHORTCUT_ID

public static final String EXTRA_SHORTCUT_ID

Key to get the shortcut ID from extras of a share intent. When user selects a direct share item from ShareSheet, the app will receive a share intent which includes the ID of the corresponding shortcut in the extras field.

Constant Value: "android.intent.extra.shortcut.ID"

Public methods

addDynamicShortcuts

public static boolean addDynamicShortcuts (Context context, 
                List<ShortcutInfoCompat> shortcutInfoList)

Publish the list of dynamic shortcuts. If there are already dynamic or pinned shortcuts with the same IDs, each mutable shortcut is updated.

This API will be rate-limited.

Parameters
context Context

shortcutInfoList List

Returns
boolean true if the call has succeeded. false if the call fails or is rate-limited.

Throws
IllegalArgumentException if getMaxShortcutCountPerActivity(Context) is exceeded, or when trying to update immutable shortcuts.

createShortcutResultIntent

public static Intent createShortcutResultIntent (Context context, 
                ShortcutInfoCompat shortcut)

Returns an Intent which can be used by the launcher to pin shortcut.

This should be used by an Activity to set result in response to Intent.ACTION_CREATE_SHORTCUT.

Parameters
context Context

shortcut ShortcutInfoCompat: new shortcut to pin

Returns
Intent the intent that should be set as the result for the calling activity

getDynamicShortcuts

public static List<ShortcutInfoCompat> getDynamicShortcuts (Context context)

Return all dynamic shortcuts from the caller app.

This API is intended to be used for examining what shortcuts are currently published. Re-publishing returned ShortcutInfos via APIs such as addDynamicShortcuts(Context, List) may cause loss of information such as icons.

Parameters
context Context

Returns
List<ShortcutInfoCompat>

getMaxShortcutCountPerActivity

public static int getMaxShortcutCountPerActivity (Context context)

Parameters
context Context

Returns
int The maximum number of static and dynamic shortcuts that each launcher icon can have at a time.

isRequestPinShortcutSupported

public static boolean isRequestPinShortcutSupported (Context context)

Parameters
context Context

Returns
boolean true if the launcher supports requestPinShortcut(Context, ShortcutInfoCompat, IntentSender), false otherwise

removeAllDynamicShortcuts

public static void removeAllDynamicShortcuts (Context context)

Delete all dynamic shortcuts from the caller app.

Parameters
context Context

removeDynamicShortcuts

public static void removeDynamicShortcuts (Context context, 
                List<String> shortcutIds)

Delete dynamic shortcuts by ID.

Parameters
context Context

shortcutIds List

requestPinShortcut

public static boolean requestPinShortcut (Context context, 
                ShortcutInfoCompat shortcut, 
                IntentSender callback)

Request to create a pinned shortcut.

On API <= 25 it creates a legacy shortcut with the provided icon, label and intent. For newer APIs it will create a ShortcutInfo object which can be updated by the app.

Use PendingIntent.getIntentSender() to create a IntentSender.

Parameters
context Context

shortcut ShortcutInfoCompat: new shortcut to pin

callback IntentSender: if not null, this intent will be sent when the shortcut is pinned

Returns
boolean true if the launcher supports this feature

updateShortcuts

public static boolean updateShortcuts (Context context, 
                List<ShortcutInfoCompat> shortcutInfoList)

Update all existing shortcuts with the same IDs. Target shortcuts may be pinned and/or dynamic, but they must not be immutable.

This API will be rate-limited.

Parameters
context Context

shortcutInfoList List

Returns
boolean true if the call has succeeded. false if the call fails or is rate-limited.

Throws
IllegalArgumentException If trying to update immutable shortcuts.