I am trying to create a pinned shortcut on the homescreen of my using ShortcutManager. I am able to create the pinned shortcut using the following code:
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("www.google.com"));
if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)){
ShortcutInfoCompat shortcutInfo = new ShortcutInfoCompat.Builder(context, "#1")
.setIntent(i)
.setShortLabel("label")
.setIcon(IconCompat.createWithResource(context, R.drawable.ic_launcher))
.build();
ShortcutManagerCompat.requestPinShortcut(context, shortcutInfo, null);
}else{
L.v("Shortcut", "Pinned shortcuts are not supported!");
}
I am facing two issues:
- There is no check to handle duplicate shortcuts. Every time I click on the button to create a shortcut, it creates a shortcut every single time and the home screen is getting filled by these shortcuts. Is there any way to check whether the shortcut already exists like:-
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
i.setData(Uri.parse("www.google.com"));
Intent installer = new Intent(); installer.putExtra("android.intent.extra.shortcut.INTENT", i); installer.putExtra("android.intent.extra.shortcut.NAME", "Shortcut name"); installer.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", Intent.ShortcutIconResource.fromContext(getApplicationContext() , R.drawable.ic_launcher));
installer.putExtra("duplicate", false);
installer.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(installer);
The problem with this piece of code is that it is not working in android 8.0 and above but it handles duplication of shortcut correctly using the following code :-
installer.putExtra("duplicate", false);
I want to achieve the same using Shortcut Manager
- When a shortcut is created using Shortcut Manager, the icon is duplicated like
I have looked at the solution provided here but no luck so far:-
Strange app icon duplication in pinned shortcut (Android O)
Any ideas??
or