5

I have an Non-Launcher activity with a context menu. The menu contains an option to add the activity to android home screen as a shortcut.

I am using the below code to create the shortcut.

private void ShortcutIcon(){

    Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.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, "Test");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));

    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    getApplicationContext().sendBroadcast(addIntent);
}

Necessary permissions and intent filters are correctly set. When I run this code, the shortcut is created successfully. On shortcut click, the activity opens as expected.

However, my activity shows some dynamic data. for that I need to pass one small string variable to the activity.

I have tried to use this code before setAction (just like you would pass extra data to a normal intent for launching an activity)

addIntent.putExtra("key_primarykey", value_i_want_to_pass);

However, when user clicks on the shortcut, inside the activity, the value_i_want_to_pass comes as Null.

Some applications like Whatsapp allows to do exactly the same. you can save a shortcut of a chat. also some dialer apps allow to add a contact as a shortcut so that when you tap on the shortcut, a voice call is initiated automatically.

I want to know how can I pass some data from my shortcut to my activity.

Bluemarble
  • 1,925
  • 4
  • 20
  • 35

1 Answers1

0

You are sending your data to addIntent which will be caught by Launcher's Broadcast Receiver.

Just change following line

addIntent.putExtra("key_primarykey", value_i_want_to_pass); 

to

shortcutIntent.putExtra("key_primarykey", value_i_want_to_pass);

and write it along with your shorcutIntent code before setting shortcutIntent to addIntent.
So that the Action Intent you are setting for the shortcut will return you the correct value. So the modified code will be as follows.

private void ShortcutIcon(){

    Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    shortcutIntent.putExtra("key_primarykey", value_i_want_to_pass);

    Intent addIntent = new Intent();
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Test");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.ic_launcher));

    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    getApplicationContext().sendBroadcast(addIntent);
}
Nikhil
  • 3,711
  • 8
  • 32
  • 43