Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I found out that in order to prevent the app from creating a duplicate of the shortcut created at app installation (or by copying from the menu), I have to create one with the same parameters of the existing one - using the same shortcut name is not enough.</p> <p>After a lot of testing and thanks to the logcat, I've been able to create an exact replica as follows:</p> <pre><code>private void installShortcut() { final Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class); shortcutIntent.setAction(Intent.ACTION_MAIN); shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER); shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); final Intent intent = new Intent(); intent.putExtra("duplicate", false); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.icon)); intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); sendBroadcast(intent); } </code></pre> <p>The difference with other answers I found on SO is in these 3 lines:</p> <pre><code> shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER); shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); </code></pre> <p>which I figured out by looking at the log entry printed when manually launching the app using the system created shortcut</p> <pre><code>Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.myapp/.activities.main.MainActivity bnds=[365,73][475,191] } from pid 279 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload