2

So, basically what I want to achieve is: get to know when I am accessing the activity through a shortcut and when not. I am using static shortcuts.

Went through the documentation but no luck. Maybe it's related more to the intent, which is a part of Android programming I am not mastering.

Thanks.

3 Answers3

3

Use an action string that is unique to the app shortcut. Note that the action string does not need to be on the <intent-filter> for the <activity> in the manifest — the <activity> does not need an <intent-filter> at all, AFAIK.

You have to have an android:action attribute in the <intent> element for the app shortcut anyway (for inexplicable reasons). So, set that to some string. Then, in your activity, you can use getIntent() to see the Intent that was used to create the activity and see if it has this particular action string. If it does, then the activity was created through an app shortcut.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
3

you can add to the android:action="android.intent.action.VIEW" intent this to each shortcut:

<intent
   <extra
         android:name="key"
         android:value="value" />
</intent>

and then in your activity check it like this :

Intent intentaction = getIntent();


if (intentaction.hasExtra("key")) {

        //Do something 
    }
Frank
  • 2,738
  • 2
  • 14
  • 19
0

Official documentatio should give you some hints though it doesn´t answers your question directly. https://developer.android.com/guide/topics/ui/shortcuts.html#tracking-usage

Another simple approach could be when dealing with dynamic shortcuts, you could add some kind of flag to the intent and check for that flag in the target activity, then you can be sure that the activity was called from the shortcut.

Hope it helps...

Alejandro Casanova
  • 3,633
  • 4
  • 31
  • 46