Search code examples
androidandroid-8.0-oreoandroid-7.1-nougatandroid-api-levelsandroid-shortcut

Pinned Shortcuts Minimum API Level in Documentation Incompatible With Code


as Documentation Says :

If your app targets Android 7.1 (API level 25) or higher, you can define shortcuts to specific actions in your app.

  • Pinned shortcuts are published at runtime and also use the ShortcutManager API. During runtime, your app can attempt to pin the shortcut, at which time the user receives a confirmation dialog asking their permission to pin the shortcut. The pinned shortcut appears in supported launchers only if the user accepts the pinning request. Link

So it should be possible to use pinned shortcuts in API level 25 itself and above, but in the code I get Error that Calls require API level 26.

It's happening for all PinShortcut Methods like :

if (android.os.Build.VERSION.SDK_INT >=  Build.VERSION_CODES.N_MR1)
            ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
            if (shortcutManager.isRequestPinShortcutSupported()) {
               //do something
            }
}

or

shortcutManager.createShortcutResultIntent(pinShortcutInfo);

these lines of code gets and API Level 26 is required error.

What is the problem? Why Document and SDK say different things? and how Can I solve this?


Solution

  • While the ShortcutManager was added in API 25, some of its methods were added later in API 26.

    You can see on the documentation that the isRequestPinShortcutSupported and createShortcutResultIntent methods were added in API 26.

    In other words:

    • In API 25 you can enable shortcuts in your app, but the user must add these shortcuts to the homescreen manually if he wants them there.
    • In API 26 you can request to add these shortcuts automatically to the homescreen, using those two new methods.