Create shortcut for specific activity on home screen

8.2k views Asked by At

I would like to give my users an option to create a shortcut to specific page within the app. I've seen similar usage at Whatsapp when you long press a chat and you are able to create a desktop shortcut to this specific chat.

I've tried finding some documentation about this functionality but couldn't get it working. Here's what I have:

activity which isn't the launcher activity (including the intent-filter)

 <activity android:name="com.my.example.pages.Topics"
    android:parentActivityName="com.my.example.pages.Apps">
        <intent-filter>
            <action android:name="android.intent.action.CREATE_SHORTCUT"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>

createShortcut function

 public void createShortcut(){
        Intent shortcutIntent = new Intent("com.my.example.pages.Topics");
        Intent.ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(getActivity(), R.drawable.app_logo);

        // The result we are passing back from this activity
        Intent intent = new Intent();
        intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Test");
        intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
        getActivity().setResult(getActivity().RESULT_OK, intent);
        getActivity().finish();

        Toast.makeText(getActivity(),"Shortcut created",Toast.LENGTH_SHORT).show();
    }

Manifest

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

I'm probably missing something since after calling the function I get the Toasts but there's no shortcut created and the app exits because of the finish() method.

To be more clearer - how do I create shortcut for non-launcher activity?

*I'm running the code within one of my viewpager fragments.

3

There are 3 answers

0
an0nym0use On

After referring to the link(pinned shortcuts), the following kotlin code worked for me

fun createWebActivityShortcut(context:Context,shortcutname:String,extra1:String) {

    val shortcutManager = ContextCompat.getSystemService<ShortcutManager>(
        context,
        ShortcutManager::class.java
    )

    if (shortcutManager!!.isRequestPinShortcutSupported) {
        val pinShortcutInfoBuilder = ShortcutInfo.Builder(context, "yourapp_"+shortcutname)
        pinShortcutInfoBuilder.setShortLabel(shortcutname)
        val intent = Intent(Intent.ACTION_VIEW, null, context, YourSpecificActivity::class.java)
        intent.putExtra("extra1",extra1) //add as many extras as you like
        pinShortcutInfoBuilder.setIntent(intent)
        pinShortcutInfoBuilder.setIcon()//add your icon here
        val pinShortcutInfo = pinShortcutInfoBuilder.build()

        val pinnedShortcutCallbackIntent = shortcutManager.createShortcutResultIntent(
            pinShortcutInfo
        )

        val successCallback = PendingIntent.getBroadcast(
            context, /* request code */ 0,
            pinnedShortcutCallbackIntent, /* flags */ 0
        )

        shortcutManager.requestPinShortcut(
            pinShortcutInfo,
            successCallback.intentSender
        )
    }
}
1
Himanshu arora On

Use this to create a shortcut for non-launcher activity.

    private void createShortcutOfApp() {

        Intent shortcutIntent = new Intent(getApplicationContext(),
            YourTargetActivity.class);
        shortcutIntent.setAction(Intent.ACTION_MAIN);

        Intent addIntent = new Intent();
        addIntent
            .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "App shortcut name");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
        Intent.ShortcutIconResource.fromContext(getApplicationContext(),
        R.mipmap.logo_of_your_app_shortcut));

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

Now add permission in manifest

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

Now define

android:exported="true"

attribute in

<activity> tag 

like

<activity
  android:name=".YourTargetActivity"
  android:exported="true"></activity>

This works like whatsapp app chat shortcut.

0
sifr_dot_in On

this works on android 14 (api 34) also

public void createLauncherDesktopShortcut(Intent intent, @DrawableRes int iconRes, String title) {
  intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  if (intent.getAction() == null) {
    intent.setAction(Intent.ACTION_VIEW);
  }
  ShortcutInfoCompat shortcut = new ShortcutInfoCompat.Builder(_context, Long.toString(new Random().nextLong()))
      .setIntent(intent)
      .setIcon(IconCompat.createWithResource(_context, iconRes))
      .setShortLabel(title)
      .setLongLabel(title)
      .build();
  ShortcutManagerCompat.requestPinShortcut(_context, shortcut, null);
}

don't forget to add permission in manifest

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

and add
android:exported="true" ahead of activity in manifest. e.g.

<activity
    android:name=".ui.sifr_in"
    android:exported="true" />

than call the function this way:

Intent shortcutIntent = new Intent(getApplicationContext(), YourClass2createShortCut.class);
createLauncherDesktopShortcut(shortcutIntent, R.mipmap.ic_launcher, "shortcut by sifr")

copied from here