While I was exploring my new Pixel XL, one of the things I noticed while setting up the perfect home screen was a pop-up menu when long pressing on certain apps. This immediately piqued my curiosity so I went looking to see what this mystery menu was; and then I discovered app shortcuts. App shortcuts are new in Android 7.1 and can be seen on Nexus and Pixel devices. You can have up to five shortcuts for your app at one time (although Google recommends four) and users of your shortcuts can also pin them to the launcher.
As it turns out app shortcuts are pretty simple! There are two types of shortcuts; the first is a static shortcut. These shortcuts are for quick, well-defined actions in your app. A common use that comes to mind is if you had a Floating Action Button, you could take that same action and use it as a static shortcut. In a live-streaming application, one example of a static shortcut could be to start a stream. Another shortcut example could be posting a message to your favorite social media site.
To create static shortcuts you first need to modify your app’s manifest. Find an activity whose intent filter action is set to android.intent.action.MAIN and category set to android.intent.category.LAUNCHER. Next, add a that specifies where your shortcuts are defined.
< meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts"/>
Static shortcuts are created by defining an XML file with the list of shortcuts.
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android"> <shortcut android:shortcutId="post" android:icon="@drawable/icn_post" android:enabled="true" android:shortcutShortLabel="@string/short_label" android:shortcutLongLabel="@string/long_label" android:shortcutDisabledMessage="@string/disabled_message"> <intent android:action="android.intent.action.VIEW" android:targetPackage="com.example.myapp" android:targetClass="com.example.myapp.MainActivity" android:data="https://myapp.com/post"/> <categories android:name="android.shortcut.post"/> </shortcut> <!-- Specify more shortcuts here. --> </shortcuts>
Note: The first intent of a manifest shortcut will always have FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK set.
The second type of shortcut is a dynamic shortcut. These shortcuts are great for contextually-relevant actions in your app. For example, in a chat application, you could show the last three people the user messaged so they can quickly message them again. The ShortcutManager API was created to dynamically create and manage shortcuts.
To create a dynamic shortcut, first get an instance of the ShortcutManager by calling getSystemService(ShortcutManager.class). Next, you need to create a ShortcutInfo object.
Intent intent = new Intent(context, MyActivity.class); intent.setAction(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); intent.putExtra(SOME_KEY, "value"); ShortcutInfo shortcut = new ShortcutInfo.Builder(App.getInstance(), uniqueId) .setShortLabel("short_label") .setLongLabel("long_label") .setIcon(Icon.createWithResource(App.getInstance(), R.mipmap.icon)) .setIntent(intent) .build();
Note: Make sure the ID that is passed into the Builder is unique, otherwise, you won’t be able to create multiple dynamic shortcuts.
Once you have your ShortcutInfo object, you can either call addDynamicShortcuts(List), setDynamicShortcuts(List), or updateShortcuts(List).
If a Shortcut is no longer relevant you can use removeDynamicShortcuts(List) to remove it from the list of shortcuts—but if a user has pinned your shortcut to the launcher, this will not remove it. Instead, you should call disableShortcuts(List) and this will remove the shortcut from the list and remove it if the user pinned it.
One of the advantages to using a dynamic shortcut is that we can set flags and extra information on our Intent. In this case, FLAG_ACTIVITY_CLEAR_TASK is specified in case the app is running—in which case, our target Activity may not get displayed.
And that’s it! App shortcuts are a great way to give users quick access to the things they love doing in your app. And, with a minimal amount of work needed to implement them, app shortcuts could be a big win for both developers and users. If you’re interested in making your own launcher and want to access shortcuts, there is the LauncherApps API. You can learn even more about app shortcuts at any of the links below!