This question may sound duplicate but there is no answer that i found is working:
I have gone through these question(s):
Android create shortcuts on the home screen
But the proposed solution is not working.
I have used the below solution which is working in API Level < 23
Intent shortcutIntent = new Intent(context,
LedgerDetailActivity.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, ledgerBean.getName());
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_action_comments));
addIntent.setAction("android.intent.action.CREATE_SHORTCUT");
addIntent.putExtra("duplicate", false);
context.sendBroadcast(addIntent);
Added Permission:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
But the above solution is not working in all devices and giving weird functionality sometimes like below:
- In some devices like Samsung Edge (with Android N) shortcuts are not getting created
- In my emulator with Android 7.1 (Android N) only one shortcut is getting created
Can someone please help me, There is no official documentation for this feature, Please don't confuse with App shortcuts introduced in Android N. I need shortcuts in home screen.
For adding shortcut first you have to add permission in Android:
and for above 23 API check for runtime permission for that use below link: Runtime Permission in Android
Now add shortcut:
If you find that multiple shortcuts created to avoid that you can check if the shortcut has been created or not: