3

我有一項服務,當用戶執行一個動作時,它被更改爲使用Service.startForeground(..)在前臺運行,因爲殺死它會對用戶造成干擾。如何重用使用startForeground創建的通知中的頂級活動?

我用下面的代碼來創建其未決的意圖建立一個通知:

Intent topActivityIntent = new Intent(this, topActivityClass); 
// Rebuild the task stack 
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
// Adds the back stack 
stackBuilder.addParentStack(topActivityClass); 
stackBuilder.addNextIntent(topActivityIntent); 
for (int i = 0; i < stackBuilder.getIntentCount(); i++) { 
    Intent currentIntent = stackBuilder.editIntentAt(i); 
    // Set flags to tell that we want to re-use the existing 
    // topActivity if possible 
    currentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
     Intent.FLAG_ACTIVITY_SINGLE_TOP | 
     Intent.FLAG_ACTIVITY_NEW_TASK); 
} 
// Adds the Intent to the top of the stack 
// Gets a PendingIntent containing the entire back stack 
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, 
    PendingIntent.FLAG_UPDATE_CURRENT); 

topActivityClass是用戶目前看到的,因爲我想他/她回來到同一活動的活動。

這裏的問題是,當用戶點擊通知時,會創建一個新的Activity而不是使用任務堆棧中的一個。

我試圖設置標誌只有topActivityIntent和標誌的幾種組合。

我應該使用哪些標記來避免創建新活動?是否有可能重新使用堆棧中的活動?

回答

0

只是從意圖刪除FLAG_ACTIVITY_NEW_TASK,它會重用現有的任務

相關問題