2

I have 2 Activities:

1.MainActicity

2.SecondActivity

I wish to create home screen shortcut to second activity:

Intent shortcutIntent = new Intent(getApplicationContext(), SecondActivity.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");
getApplicationContext().sendBroadcast(addIntent); 

When I click the shortcut it does nothing and I get toast from my launcher "Unable access the application". But when I add:

<intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>

To my second activity, it works just how I wish it to do, but now I have 2 launcher icons in my application.

What I wish is that I`ll have one launcher application and home screen shortcut to my second activity.

Dim
  • 4,527
  • 15
  • 80
  • 139

2 Answers2

1

This is exactly how it works. You cannot add shortcut to actvity that cannot be launched because it simply makes no real sense. And to allow user to start that activity by hand, said intent-filter is mandatory.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • 1
    Sow maybe there is possibility to hide she second activity from the app drawer? – Dim Jan 03 '14 at 11:00
  • 1
    default launcher does not allow that. Other may let you to, but by default all entries with `intent-filter` shall be visible because this is the main purpose of having `intent-filter` set – Marcin Orlowski Jan 03 '14 at 11:09
  • It doesn't have to be second LAUNCHER – SagiLow Jan 20 '17 at 17:18
  • Thanks @SagiLow for downvote. Mind explaining what are you talking about? – Marcin Orlowski Jan 20 '17 at 20:26
  • @MarcinOrlowski, as I wrote, the activity doesn't have to be launcher activity in order to be launched directly from a shortcut, it is enough to add `android:exported="true"` and it would solve the problem he experienced without having 2 launcher icons. – SagiLow Jan 20 '17 at 22:51
0

Try removing LAUNCHER as a category (replace with DEFAULT). That's what's sticking it in the app drawer.

Lawrence Kesteloot
  • 4,149
  • 2
  • 31
  • 28