views:

3319

answers:

4

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 something else, then use one of the shortcuts to jump back to that Activity. Since the app is still in memory it just opens the new Activity on top of the others and the "Back" key will take them back through the whole history. What I'd like to have happen is if they use a shortcut then to effectively kill the history and have the back key just exit the app. Any suggestions?

+1  A: 

Try adding Intent.FLAG_NEW_TASK to the Intent.

Isaac Waller
Well, that got me part of the way there at least. Thanks
fiXedd
A: 

fiXedd, I want to implement a shortcut to launch an app just like what you did. I am reading the code of Activity.onKeyDown() and Launcher.onKeyDown(). Can you share your source code for the shortcut or share your ideas on it? Thank you very much in advance.

A: 

fiXedd, seems what you did is not what I want to do. I want: user can press a key sequence like ##123## on home screen (idle screen), then my special activty will be lanched instead of dialer activity or contacts activity. Any suggestion?

Oh man, I don't have any idea. Sorry. I was just putting an icon on the Home screen that led to a specific part of my app. Do share if you figure it out though.
fiXedd
Look at android.provider.contacts.UI.Intents
Isaac Waller
+3  A: 

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);
fiXedd