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.
After referring to the link(pinned shortcuts), the following kotlin code worked for me