Android ShortCutManager replaces static shortcut by dynamic one

1k Views Asked by At

I have 4 static shortcuts defined im my shortcuts.xml (static1, static2, static3, static4) - all are enabled. As soon as I add a dynamic shortcut (dynamic1), the last static shortcut (static4) is overridden by the dynamic one. Thereby, the launcher now displays static1, static2, static3, static4 instead of static1, static2, static3, static4. When I add a second dynamic shortcut (dynamic2) it overrides the static3 as well.

In the ShortcutManager documentation it is written clearly that static shortcuts are always ranked higher (rank == 0) than dynamic ones and thereby, should be shown before dynamic ones.

https://developer.android.com/guide/topics/ui/shortcuts/managing-shortcuts#display-order

When the launcher displays an app's shortcuts, they should appear in the following order:

  1. Static shortcuts: Shortcuts whose isDeclaredInManifest() method returns true.
  2. Dynamic shortcuts: Shortcuts whose ShortcutInfo.isDynamic() method returns true. Within each shortcut type (static and dynamic), shortcuts are sorted in order of increasing rank according to ShortcutInfo.getRank().

I'm not sure what is going wrong here and how I can fix it to always show the static shortcuts. The purpose of my dynamic shortcuts is to provide direct share functionality.

Here is how I add my dynamic ShortCut(s):

    final ArrayList<ShortcutInfoCompat> shortcuts = new ArrayList<>();
    final Set<String> categories = Collections.singleton("my.package.category.SOME_CATEGORY");

    final ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(context, "shortcutId")
            .setShortLabel("some_label")
            .setIcon(IconCompat.createWithResource(context, R.drawable.some_icon))
            .setIntent(new Intent(Intent.ACTION_DEFAULT))
            .setCategories(categories)
            .setPerson(
                    new Person.Builder()
                            .setName("some_name")
                            .build()
            )
            .build();

    shortcuts.add(shortcut);

    ShortcutManagerCompat.addDynamicShortcuts(context, shortcuts);
1

There are 1 best solutions below

4
On

While it's true that static shortcuts are always before the dynamic ones, from reading the documentation, there seems to be no guarantee on the amount of shortcuts that are displayed on each group.

There are several factors you should consider from the documentation:

Shortcut limitations

Although you can publish up to five shortcuts (static and dynamic shortcuts combined) at a time for your app, most launchers can only display four.

So what should android do when you add a fifth dynamic shortcut in addition to the four static ones? You'd never be able to show the dynamic one to the user if all the static ones are displayed.

Shortcut count limitation

Each app's launcher icon can contain at most getMaxShortcutCountPerActivity() number of static and dynamic shortcuts combined. There is no limit to the number of pinned shortcuts that an app can create, though.

So watch out for that maximum, as you won't be able to add more than that anyways.

Best practices

Publish only four distinct shortcuts
Although the API currently supports a combination of up to five static and dynamic shortcuts for your app at any given time, we recommend that you publish only four distinct shortcuts to improve their visual appearance in the launcher.


I can't tell you what you should do, but having that many static and dynamic shortcuts is simply not possible. While it is possible to have that many shortcuts, it's up to the launcher to choose what to display. Therefore you should consider the recommended maximum of 4. You may reconsider what shortcuts you deem essential and which ones you do not.

Another approach may be to use Pinned Shortcuts as an alternative.