2

I'm trying to find the way to create a menu with shortcuts.

For example my Activity loads a fragment depends on the data that comes on a Intent:

enter image description here

I got this method from this topic:

private void addShortcut() {
    //Adding shortcut for MainActivity 
    //on Home screen
    Intent shortcutIntent = new Intent(getApplicationContext(),
            MainActivity.class);

    shortcutIntent.setAction(Intent.ACTION_MAIN);

    Intent addIntent = new Intent();
    addIntent
            .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                    R.drawable.ic_launcher));

    addIntent
            .setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    addIntent.putExtra("duplicate", false);  //may it's already there so don't duplicate
    getApplicationContext().sendBroadcast(addIntent);
}

So, if I try to create 2 shortcuts for MainActivity, it gives me "the shortcut is already created".

Thank you.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Fabio Venturi Pastor
  • 2,519
  • 3
  • 19
  • 32
  • Hope the below link may give you some clues. [https://stackoverflow.com/questions/11240023/two-launchers-for-a-single-activity](https://stackoverflow.com/questions/11240023/two-launchers-for-a-single-activity) – TheHound.developer Dec 12 '17 at 12:25
  • did you found any solution to create multiple shortcuts? – Awais Oct 07 '18 at 17:40

1 Answers1

1

Creating multiple shortcut try this.

Intent shortcutIntent;
shortcutIntent = new Intent();
shortcutIntent.setComponent(new ComponentName(activity.getPackageName(), ".classname"));

shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

final Intent putShortCutIntent = new Intent();
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);

// Sets the custom shortcut's title
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"Title");
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(PersonProfile.this, R.drawable.icon));
putShortCutIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(putShortCutIntent);

add the permission in manifest.

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />

Happy coding!!

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49