Skip to content

App Shortcut

App Shortcut

The maximum actions for an app is 5

Can’t be modified after build

In AndroidManifest add meta data inside activity:

<meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" />

Add actions in res/xml/ for example res/xml/shortcuts.xml

<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:icon="@drawable/event"
android:shortcutId="events"
android:shortcutShortLabel="@string/event"
android:shortcutLongLabel="@string/event_long"
>
<intent
android:action="android.intent.action.VIEW"
android:targetClass="dev.eliaschen.wsc2022maam.MainActivity"
android:targetPackage="dev.eliaschen.wsc2022maam">
<extra
android:name="shortcut_id"
android:value="events" />
</intent>
</shortcut>
</shortcuts>

android:shortcutShortLabel and android:shortcutLongLabel(optional) has to be a string resource

val customIntent = Intent(
context,
MainActivity::class.java
).apply {
action = Intent.ACTION_VIEW
putExtra("shortcut_id", "events")
}
val shortcut = ShortcutInfoCompat.Builder(context, "id1")
.setShortLabel("Dynamic Event")
.setLongLabel("Open the events")
.setIcon(
IconCompat.createWithResource(
context,
R.drawable.bookmark
)
) .setIntent(customIntent)
.build()
ShortcutManagerCompat.pushDynamicShortcut(context, shortcut)

since we put a extra in intent , just check the extra in MainActivity like you normally do

intent.getStringExtra("shortcut_id")