Android Dynamic Shortcuts.

Satyam Gondhale
Nerd For Tech
Published in
2 min readJun 28, 2019

--

Android Shortcut is introduced in Android 7.1, Api Level 25 and Higher. We can view App shortcut available for it on Long press of App Launcher icon. The main purpose of App Shortcut is to directly jump to App content screen immediately without moving through all hierarchy. It become easy to directly view what we need rather than going through all flow from App Splash screen to desired Destination.

There are two types of Shortcuts we can use for our App.

  1. Static Shortcuts : These are the shortcuts that are static and cannot be changed or modified runtime. They are declared in App Manifest and separate shortcut menu XML is created for it.
  2. Dynamic Shortcuts : These shortcuts are manually added and removed at runtime using Java and ShortcutManager Api. These Shortcut can change between uses of your app, and they can change even while your app is running

The main purpose of this article is to get familiar with basic usage of adding and removing Dynamic Shortcut runtime using ShortcutManager Api.

I have created one demo app, which will illustrate,

  1. How to create Dynamic Shortcut.
  2. How to open Specific Destination on Shortcut Click.
  3. How to add new Shortcut to List of some previously created Shortcut.

For Demo Purpose, I have added two Buttons on FragmentOne. Button1 click create shortcut One and Button2 click create shortcut two.

To create Shortcut we need to check if app version is greater than Android 7.1. ShortcutManager & ShortcutInfo are used to create shortcut. The main parameters required while creating Shortcut is shortLabel, longLabel, icon, and action.

To Handle destination, after user clicks shortcut we need to get Actions from Intent and based on this we need to open specific screen of app. Please don’t forget to setAction in setIntents while creating Shortcut.

There might be a case where we want to add new Shortcut Dynamically to previously added Shortcut List. To do this we need to check if there is already any shortcut added and add current shortcut to the list.

The complete source code is available here for your reference.

Than you. Feel free to share your Thoughts and Claps.

--

--