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.
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
ShortcutManagerAPI. These can be published, updated, and removed at runtime. Show top 3 frequent chats is a example of dynamic shortcut.
To add a static shortcut,
In app manifest file, find an activity whose intent filter action is set to
android.intent.action.MAINand category set to
element to this activity that references the resource file where the app's shortcuts are defined
- Create a new resource file: res/xml/shortcuts.xml
<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.
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.
setDynamicShortcuts(), will redefine the entire list of dynamic shortcuts, and
addDynamicShortcuts()will add item to existing list.
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:
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.