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.
INSTALL_SHORTCUT which can be used to add shortcuts to home screen. In following code snippet we create a shortcut of activity MainActivity with the name HelloWorldShortcut. First we need to add permission INSTALL_SHORTCUT to android manifest xml. The addShortcut() method creates a new shortcut on Home screen.
Add shortcuts to Home screens Swipe up or down to access the Apps screen. There are multiple ways to add shortcuts to the Home screen: Touch and hold the app until a menu displays, and then tap Add to Home. Touch and hold the app until the Home screen is displayed, and then release the app in your desired location.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With