Dim
Dim

Reputation: 4807

Home screen shortcut to activity

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.

Upvotes: 2

Views: 987

Answers (2)

Lawrence Kesteloot
Lawrence Kesteloot

Reputation: 4348

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

Upvotes: 0

Marcin Orlowski
Marcin Orlowski

Reputation: 75609

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.

Upvotes: 1

Related Questions