I have a Non-Launcher function with a context menu. The menu has the option of adding activity to the Android main screen as a shortcut.
I use the code below to create a 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); }
The necessary permissions and intent filters are correctly set. When I run this code, the shortcut is created successfully. When you click on a shortcut, the activity opens as expected.
However, my activity shows some dynamic data. for this I need to pass one small string variable into action.
I tried to use this code before setAction (just like you would pass extra data to a normal intent to trigger an activity)
addIntent.putExtra("key_primarykey", value_i_want_to_pass);
However, when the user clicks on the shortcut, inside the action, value_i_want_to_pass appears as Null.
Some applications, such as Whatsapp , allow you to do the same. You can save the chat shortcut. Also, some dialer apps allow you to add a contact as a shortcut so that when you click on the shortcut, a voice call starts automatically.
I want to know how to transfer some data from my shortcut to my activity.