Pick the fix
Tapping any of three notifications always opens item 1. What's the fix?
fun openItem(context: Context, id: Int): PendingIntent {
val intent = Intent(context, DetailActivity::class.java).putExtra("id", id)
return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
}Hint 1 of 1
How does the system decide two PendingIntents are the same?
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A Use a distinct request code per item (and FLAG_IMMUTABLE)
Correct. PendingIntents that match except for extras are treated as the same.
Why not the others?
- B Use FLAG_MUTABLE. Mutability doesn't make the tokens distinct.
- C Add
FLAG_ACTIVITY_NEW_TASK. That's about the task, not which PendingIntent is reused. - D Post the notifications with a delay. Timing doesn't matter; matching does.