How do you make an Android “Home” shortcut bypass the app it's point to's history?

前端 未结 2 1671
轮回少年
轮回少年 2021-02-06 18:26

I have an app that allows you to create Home \"shortcuts\" to a specific Activity. It turns out that some of my users will use the app, hit the home key to go do so

2条回答
  •  遥遥无期
    2021-02-06 18:40

    First, set up the taskAffinity in the Manifest to make the Activity run as a different "task":

    
            
                
                
            
    
    

    then, when building the shortcut, set the FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TOP flags. Something like:

    // build the shortcut's intents
    final Intent shortcutIntent = new Intent();
    shortcutIntent.setComponent(new ComponentName(this.getPackageName(), ".IncomingShortcutActivity"));
    shortcutIntent.putExtra(EXTRA_STOPID, Integer.toString(this.stop_id));
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    final Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    // Sets the custom shortcut's title
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, custom_title);
    // Set the custom shortcut icon
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.bus_stop_icon));
    // add the shortcut
    intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
    sendBroadcast(intent);
    

提交回复
热议问题