How to make Android’s “Home” shortcut bypass an app pointing to a story?

I have an application that allows you to create Home shortcuts to a specific one Activity. It turns out that some of my users will use the application, click on the home key to do something else, and then use one of the shortcuts to return to this operation. Since the application is still in memory, it simply opens a new one Activityon top of the others, and the Back key returns them to the whole story. I would like this to happen if they use a shortcut to effectively kill the story and get the key back from the application. Any suggestions?

+3
source share
2 answers

First configure taskAffinity in the manifest to make it Activityrun as another "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 creating a shortcut, set the FLAG_ACTIVITY_NEW_TASKand flags FLAG_ACTIVITY_CLEAR_TOP. Sort of:

// build the shortcut 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 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);
+7
source

Try adding Intent.FLAG_NEW_TASKto the intent.

+1
source

Source: https://habr.com/ru/post/1710206/


All Articles