Skip to content

Instantly share code, notes, and snippets.

@fnk0
Last active October 25, 2016 06:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fnk0/0348c707785211d7850f7a9fe4eb0ad0 to your computer and use it in GitHub Desktop.
Save fnk0/0348c707785211d7850f7a9fe4eb0ad0 to your computer and use it in GitHub Desktop.
Shortcut Manager how to
// Somewhere inside your app...
if (isUserLoggedIn() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
// NOTE: You MUST specify an Action on the intents. Otherwise it will crash
Intent home = new Intent(this, HomeActivity.class);
home.setAction(Intent.ACTION_VIEW);
Intent profile = new Intent(this, UserProfileActivity.class);
profile.setAction(Intent.ACTION_VIEW);
Intent[] intents = new Intent[] {home, profile};
ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "profile")
.setIntents(intents)
.setShortLabel("Profile")
.setLongLabel("Open User Profile")
.setIcon(Icon.createWithResource(this, R.drawable.ic_social_person_primary))
.build();
// I'm just adding one shortcut here. But a limit tof up to 5 shortcuts can be set to a specific app.
// Note: the limit of 5 shortcuts are for static + dynamic shortcuts. The app will always show the static one
// First. So if an App has 2 static shortcuts for example, it would be able to have up to 3 dynamic ones.
// This API should also be called with CARE. aKa do not span this within short periods of time.
shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment