In the latest version of Android, Nougat 7.1 (API 25), Google introduced a new feature called App Shortcut. This allows you to create shortcut for specific action or screen in your app that can be opened directly from launcher to associated screen.

Shortcuts let your users quickly start common or recommended tasks within your app. These shortcuts can be displayed in a supported launcher such as Google Launcher or Pixel Launcher. To view these shortcuts, just long-press the launcher icon of the app. You can have up to five shortcuts for your app at one time and users can also pin them to the launcher.

Examples of such actions could be, navigating users to a particular location in a map app, sending messages to a friend in a communication app, Start playing particular playist in music app etc.

Android App Shortcut Sample

Adding App Shortcuts

You can add two different types of shortcuts for your app:

  • Static Shortcut : These shortcuts are quick and well defined in your app. They should provide generic actions in app and these actions should remain consistent over the lifetime of your app’s current version. Example of such action could be Create New Message as this action is well defined.

  • Dynamic Shortcut : Dynamic shortcuts should provide links to specific, context-sensitive actions within your app. They are published at runtime using the ShortcutManager API. These can be published, updated, and removed at runtime. Show top 3 frequent chats is a example of dynamic shortcut.

Static Shortcut

To add a static shortcut,

  • In app manifest file, find an activity whose intent filter action is set to android.intent.action.MAIN and category set to android.intent.category.LAUNCHER

  • Add a element to this activity that references the resource file where the app's shortcuts are defined

<meta-data android:name="android.app.shortcuts"
           android:resource="@xml/shortcuts" />
  • Create a new resource file: res/xml/shortcuts.xml
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
    <shortcut
        android:shortcutId="new_message"
        android:enabled="true"
        android:icon="@drawable/ic_shortcut_new_message"
        android:shortcutLongLabel="New Message"
        android:shortcutShortLabel="New Message"
        android:shortcutDisabledMessage="This shortcut is no longer available." >
        <intent
            android:action="android.intent.action.VIEW"
            android:targetClass="com.cybrilla.AppShortcut.NewMessageActivity"
            android:targetPackage="com.cybrilla.AppShortcut.newmessage" />
    </shortcut>
</shortcuts>

Each <shortcut> element, in turn, contains information about a static shortcut, including its icon, its description labels, and the intents that it launches within the app. android:shortcutLongLabel attribute text is shown when user pin that shortcut. If any shortcut is disables, but its already pinned, then message in android:shortcutDisabledMessage is shown when user tap it.

Dynamic Shortcut

These actions can change between uses of your app, and they can change even while your app is running. ShortcutManager API is used to create and manage shortcuts at runtime. You can either publish, update or remove dynamic shortcut at runtime.

  • Publish: Using setDynamicShortcuts(), will redefine the entire list of dynamic shortcuts, and addDynamicShortcuts() will add item to existing list.

  • Update: Using updateShortcuts() method you can update shortcut.

  • Remove: If a Shortcut is no longer relevant you can use removeDynamicShortcuts() to remove it from the list of shortcuts

Using following code, you can dynamically create a shortcut:

ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id")
    .setShortLabel("Cybrilla Technologies")
    .setLongLabel("Go to Cybrilla Technologies Website")
    .setIcon(Icon.createWithResource(context, R.drawable.ic_cybrilla_technologies))
    .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.cybrilla.com/")))
    .build();

shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));

Conclusion

Using app shortcuts, you can surface key actions and take users deep into your app instantly. They are great way to give users quick access to the things they love doing in your app. As very little amount of code is required to implement them, its win win for both users and developers.