ユーザーがアクションを実行すると、 Service.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
およびフラグのいくつかの組み合わせのみにフラグを設定しようとしました。
新しいアクティビティを作成しないようにするには、どのフラグを使用すればよいですか? スタック内のアクティビティを再利用することはできますか?