2009-06-10 46 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

相關問題