2009-06-10 49 views
3

我有一个应用程序,允许您创建一个特定的Activity首页“快捷方式”。事实证明,我的一些用户将使用该应用程序,点击主键以执行其他操作,然后使用其中一个快捷键跳回该活动。由于该应用程序仍在记忆中,因此只需在其他人的基础上打开新的Activity,“后退”键将使他们回到整个历史记录。我想要发生的事情是,如果他们使用快捷方式然后有效地杀死历史记录并使后退键退出应用程序。有什么建议么?如何使Android“家庭”快捷方式绕过应用程序,直指历史?

回答

7

首先,建立taskAffinity的清单,使Activity运行方式不同的“任务”:

<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> 

然后,建立快捷方式时,设置FLAG_ACTIVITY_NEW_TASKFLAG_ACTIVITY_CLEAR_TOP标志。像这样:

// 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

请您澄清一点吗?代码应该去哪里,IncomingShortcutActivity?谁来处理广播? – Maxim 2011-08-08 20:26:25

1

尝试将Intent.FLAG_NEW_TASK添加到意图。

+0

那么,至少让我成为那里的一部分。谢谢 – 2009-06-11 03:00:45

相关问题