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

前端 未结 2 1672
轮回少年
轮回少年 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":

    <activity
            android:taskAffinity="" 
            android:name=".IncomingShortcutActivity">
            <intent-filter>
                <action android:name="com.example.App.Shortcut"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
    </activity>
    

    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);
    
    0 讨论(0)
  • 2021-02-06 18:47

    Try adding Intent.FLAG_NEW_TASK to the Intent.

    0 讨论(0)
提交回复
热议问题